Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been successfully using wcf webservices to do some simple operations. Now I'm trying to pass a more complex object using jquery ajax post but this time I have always the same error (bad request). I have searched a lot but still could not identify the problem

Here's what I'm trying to do:

My interface

[OperationContract(Name = "PersonAddress")]
[WebInvoke(UriTemplate = "AddPersonAddress/", Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
PersonAddress AddPersonAddress(PersonAddress objPA);

My Javascript test code:

    var testObj = {

        cpostal: "Postal 1",
        address: "My address",
        zone:""


    }

    var objectAsJson = JSON.stringify({ objPA: testObj });


    $.post('../../App_Services/DataService.svc/PersonAddress/', objectAsJson, function (data) {
        alert("success");
    });

PersonAddress structure

public class PersonAddress
{
    public string cpostal { get; set; }
    public string address: { get; set; }
    public string zone { get; set; }

}

Chrome console post

{"objP":{"cpostal":"Postal 1","address": "My address","zone":""}}

according to these articles:

Link 1 Link 2

I have to send a string that is the JSON representation of a JS object which properties matches the parameter name of the function that i'm calling.

share|improve this question
    
Why do you use a success callback + .done? –  Johan Jul 15 '13 at 23:00
    
What is the structure of Person? You also spelled address as "adddess" in testObj. –  Edward Jul 15 '13 at 23:00
    
Try enabling tracing at the server side; the traces should tell why the server is considering the request to be bad. –  carlosfigueira Jul 15 '13 at 23:05
    
@Edward adddess is just a big typo :). I fix this but the problem remains. I will post the structure –  Sandcar Jul 15 '13 at 23:09
    
@Johan is just sample code from jquery docs for ajax post . please igonore that part . –  Sandcar Jul 15 '13 at 23:13

2 Answers 2

I would suggest changing below line :

var objectAsJson = JSON.stringify({ PersonAddress: testObj });// This is adding a property "PersonAddress" to the object

To

var objectAsJson = JSON.stringify(testObj);// this is PersonAddress object itself
share|improve this answer
    
thanks for the reply, but unfortunately it does not work. I edited the post with new information. –  Sandcar Jul 16 '13 at 13:35
up vote 0 down vote accepted

after pulling a lot of hair :) finally managed to get it to work. basically have to use the full form of $. ajax and not $. post, in order to indicate the contenttype "application / json; charset = utf-8". Now works without realizing why this happened. Thanks to all who tried to help

Code that works for me

    $.ajax({
        type: "POST",
        url: "../../App_Services/DataService.svc/PersonAddress",
        data: objectAsJson,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
       // processdata: true,
        success: function (data) {
            alert("ok")
        }
    });
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.