Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
183
rated 0 times [  187] [ 4]  / answers: 1 / hits: 73985  / 13 Years ago, tue, january 31, 2012, 12:00:00

Instead of sending a list of key/value pairs, I need to send a JSON string as the body of the POST request.

I make this POST request using jQuery's $.ajax function.

How do I set it correctly?



When I say JSON string, I mean something like: {action:'x',params:['a','b','c']} .



This is how I would use this JSON string in PHP on the server:



var_dump(json_decode(file_get_contents('php://input')));


Results in:



stdClass Object
action = x
params = Array
(
0 = a
1 = b
2 = c
)

More From » jquery

 Answers
4

Try:


$.ajax('url',{
'data': JSON.stringify(yourJSONObject), //{action:'x',params:['a','b','c']}
'type': 'POST',
'processData': false,
'contentType': 'application/json' //typically 'application/x-www-form-urlencoded', but the service you are calling may expect 'text/json'... check with the service to see what they expect as content-type in the HTTP header.
});

[#87707] Monday, January 30, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;