sending json data to server
I have this code to send data:
function insert_new_image()
{
var ids = [];
$('.be-imagecontainer').each(function(i){
ids[i] =$(this).find('span').text();
});
var jsonids = {'ids' : ids}
var url = base_url + '开发者_运维知识库ajax/get_new_images';
$.ajax({
url : url,
data : {'ids' : jsonids},
success : function(response)
{
$('#be-images ul').prepend(response)
},
dataType: 'html'
});
}
and this code from the server(PHP):
function get_new_images()
{
$ids = json_decode($_GET['ids'], true);
echo Kohana::debug($ids);
}
the server responds with a
null
how can I make this work? I am trying to send a json data containing an array of ids to the server
Use json2.js to encode the Javascript object as JSON:
data: {ids: JSON.stringify(jsonids)};
See this post for more information.
The data you send back isn't in JSON format, it should be a PHP array by the time your script reads it.
Either use : var jsonids = "{'ids' : ids}"
(in JS) or $ids = $_GET['ids'];
(in PHP)
Have you var_dump the $_GET variable? Maybe you aren't sending the right thing. Also check php.log
Another question, Kohana what kind of thing is responding?
精彩评论