我有一个对 php 的 jQuery Ajax 调用,给出了 404 未找到。我缺少什么? 从浏览器请求页面(非 Ajax)会返回 json 数据。
jQuery通过Ajax调用php:
$.post({
url:'/jqgrid/nwproducts.php',
success:function(data){
$('#auto').autocomplete({source:data.ProductName});
}
});
PHP 代码:
<?php
header("Content-Type: application/json");
header("HTTP/1.1 200 OK");
$arrayProduct = array();
$mysqli = new mysqli('localhost','login','passwd','northwind');
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$resultAll = $mysqli->query('select ProductName from products');
if (!$resultAll)
{
echo "error\n";
} else {
while ($obj = $resultAll->fetch_object()) {
array_push($arrayProduct,$obj);
}
echo json_encode($arrayProduct);
}
?>
我在 firebug 中收到以下错误:
http://localhost/jqgrid/%5Bobject%20Object%5D 404 Not Found
最佳答案
将 url
和 success
参数从对象中取出。
$.post('/jqgrid/nwproducts.php', function(data) {
$('#auto').autocomplete({source:data.ProductName});
});
关于php - jQuery ajax 调用 php 给出 404 not found : what am I missing?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8937863/