0

我创建了一个简短的示例POST

var p = {Name: obj.Name,  Age: obj.Age},
    str = JSON.stringify(p);

$.post("test.php", str, function (data) {
    alert("Post finished");
}).success(function () {
    alert("second success");
}).error(function() { 
    alert("error");
});

404 not found当我运行上面的代码时,它会抛出from的错误消息test.php

test.php我在我的项目中创建了这个文件。

为什么我会收到此错误?

4

2 回答 2

1

The first parameter in post method is the url that you would want to send the post data.

HTTP 404 means that url given is not found in the server.

Since there is no test.php that would receive your data, you will get this error.

You should create a test.php file that you accept your request.

Another thing is, you don't need to stringify JSON object while sending it to the server, jquery handles this automatically. Just send the obj as post data.

var p = { Name : "John",  Age : 23};
$.post("test.php", p).done(function(data){   
     alert("Post finished");    
}).success(function() {
    alert("second success");
}).error(function() {
    alert("error");
});
于 2012-08-26T14:03:52.067 回答
0

该错误意味着找不到该页面。你确定它存在吗?它是否存在于文档根目录中,或者您的意思是当前文件夹:

$.post("/test.php", str , function(data){ 
        ^

您是否使用 FireBug for Firefox 或 Chrome 的开发者工具检查了请求?如果您尝试使用您的用户代理直接访问该 URL,会发生什么?

于 2012-08-26T13:56:19.003 回答