Jump to content

post json with ajax


stijn0713

Recommended Posts

i want to post a json literal with jquery ajax method.

 

The $_POST array on the server side is empty, thought i see with firebug that the json literal has been send.

 

If i switch to GET method, i do receive the literal but it's not clean formatted.

 

client-side

$.ajax({
type: "GET",
url: 'ajax/saveInfoAuthenticatedUser.php',
data: pic_data,
dataType: 'json',
success: function (data) {
alert(data);
} 
});

server-side

$data = $_POST['pic_data'];

print_r($_POST);

What is the problem? I've read alot of posts on stackexchange where people had the same problem, but nowhere i could find the reason why the post variable is empty.

 

Extra question:

What is the clean way to send a json format? should i use stringify(), json_encode or something?

Link to comment
https://forums.phpfreaks.com/topic/279668-post-json-with-ajax/
Share on other sites

I think you need to specify both the name of the variable client side, and what it will appear as server side.

 

So where you have this in your ajax function:

data: pic_data

change it to:

data: {pic_data: pic_data}

Oh, and also make sure that you set the type back to POST.

 

And when returning information from the server, I always use json_encode().

 

Denno

Link to comment
https://forums.phpfreaks.com/topic/279668-post-json-with-ajax/#findComment-1438440
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.