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?

Edited by stijn0713
Link to comment
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

Edited by denno020
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.