I have a JSON object like this (assigned to var card;
):
{
card_no: "1",
card_name: "wwwgdefonru",
img_id: 1,
img_thumb: "albums/070915_E239/thumbs/001_wwwgdefonru.jpg",
img_hires: "albums/070915_E239/thumbs_hires/001_wwwgdefonru.jpg"
}
I want to pass this using AJAX to a php support file.
Here's my AJAX call:
jQuery.ajax({
method: 'POST',
url: ajax_file,
type: 'JSON',
data: card,
dataType: 'html',
cache: false,
success: function(html){
console.log('SUCCESSO');
jQuery('#debug').html(html);
},
complete:function(){
console.log('COMPLETE');
}
});
In my PHP file (just for debugging purposes) I output the passed data like so:
echo '<pre>';
print_r($_POST);
echo '</pre>';
The AJAX call completes successfully. But the output is blank:
Array
(
)
Where am I going wrong with this?
SOLUTION
I am working on a project with an old version of jQuery (don't ask...). From looking at the docs (http://api.jquery.com/jquery.ajax/) I could see that setting the method using method: 'POST'
wasn't working because it isn't supported by the version of JQ I am using. Switching it to type: 'POST'
and getting rid of type: 'JSON'
fixed it.
I wouldn't have spotted this had I not looked at the network tab and seen the request Method was GET
even though I have defined it as POST
.