0

I'm using JQuery to consume a WCF Service. Actually this works fine:

var para = ' { "Parameter" : { "ID" : "5", "Name" : "Peter" } }'
$.ajax({
   type: "POST",
   contentType: "application/json",
   data: para,
   url: url
   success: success
});

But I don't want to pass the data parameter as String and I think it should be possible to pass ist as array in any way. Like that:

var para = { "Parameter" : { "ID" : 5, "Name" : "Peter" } }

But when I try this I'm getting an error. What I'm doing wrong?

Thanks

2 Answers 2

1
var para = '{ "ID" : "5", "Name" : "Peter" }';
$.ajax({
   type: "POST",
   data: para,
   url: url
   success: success
});

If you format it like this you should be able to get the values as

$_POST will return array('ID' => '5', 'Name' => 'Peter');

but you can also access it by doing:

$_POST['ID'] and $_POST['Name']

Also you could make use of the jquery post function:

var para = '{ "ID" : "5", "Name" : "Peter" }';
$.post(
    url, 
    para
);
Sign up to request clarification or add additional context in comments.

Comments

0

You can use JSON.stringify function from the json2.js. Then you ajax call will be

var para = { Parameter : { ID :5, Name : "Peter" } };
$.ajax({
   type: "POST",
   contentType: "application/json",
   data: JSON.stringify(para),
   url: url
   success: success
});

The usage of manual conversion to the JSON string is not good because of possible spatial characterless in the string which must be escaped (see http://www.json.org/ for details).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.