我一直试图让我的 AJAX 工作几天,但我不明白为什么它不起作用。它不会发出任何警告并给我一个错误:Failed to load resource: the server responded with a status of 404 (Not Found) - [object%20Object]
它还不断返回所有内容 NULL、JS、PHP。不知道为什么。
JS
var fullName = ["John Doe", "Jane Doe"];
$(window).load(function(){
getList();
});
function getList(){
$.getJSON({
type: "GET", /* the request's method: */
url:"/names.php", /* the request's location: */
data: JSON.stringify({names: fullName}), /* the request's fields: */
contentType: "application/json; charset=utf-8", /* the request's content-type : */
dataType:"json", /* the response's content-type: */
success: function(json){ /* the callback function */
if(json.length > 0){
$.each(json, function(i, v){
console.info(v);
});
}
else {
alert('wtf?!');
}
}
});
}
PHP
<?php
$req=array_merge($_GET, $_POST);
$names=json_decode($req['names'], true);
header('content-type: application/json; charset=utf8;');
echo json_encode($names);