$.getJSON not working.. at all
I'm trying to feed a PHP array over to jQuery using JSON but am not having much luck.
$(".add-size").click(function(event) {
$("#is_ajax").val('true');
var dataString = 'ID=' + $("#product_ID").val() + '&size=' + $(this).val() + '&is_ajax=' + $("#is_ajax").val();
$.getJSON( "../bin/func/actions/add.action.php", dataString, function(data) {
$("#runningQuantity").text(data.runningQuantity);
$("#runningTotal").text(data.runningTotal);
});
event.preventDefault();
});
is the jQuery that's not working well.
$list['runningTotal'] = $runningTotal;
$list['runningQuantity'] = $runningQuantity;
echo json_encode( $list );
is the relevant PHP in add.action.php and the string this creates seems to be formatted OK. eg:
{"runningTotal":90,"runningQuantity":18}
But nothing happens in either #runningTotal or #runningQuantity, they just st开发者_如何转开发ay the same? I may be totally missing the point here.
Per Jquery's Documentation the data to be sent to server has to be in key value pairs. You can do one of two things:
$.getJSON( "../bin/func/actions/add.action.php", {Id : $("#product_ID").val(), .....}, function(data) {
$("#runningQuantity").text(data.runningQuantity);
$("#runningTotal").text(data.runningTotal);
});
or add the datastring to the URL by adding a ? so that it becomes a querystring.
$.getJSON( "../bin/func/actions/add.action.php?" + datastring, function(data) {
$("#runningQuantity").text(data.runningQuantity);
$("#runningTotal").text(data.runningTotal);
});
HTH
精彩评论