Javascript jquery中的post问题
我创建了一个Javascript jquery中的post问题,javascript,jquery,Javascript,Jquery,我创建了一个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"); }
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");
});
当我运行上述代码时,它从test.php
抛出一条错误消息404notfound
我在我的项目中创建了这个文件test.php
为什么会出现此错误?该错误意味着找不到该页面。你确定它存在吗?它是否存在于文档根目录中,还是指当前文件夹:
$.post("/test.php", str , function(data){
^
您是否使用Firefox的FireBug或Chrome的开发工具检查了请求?如果您尝试使用用户代理直接访问该URL,会发生什么情况?方法中的第一个参数是希望发送
post数据的URL
HTTP404
表示在服务器中找不到给定的url
由于没有test.php
可以接收您的数据,因此您将收到此错误
您应该创建一个接受请求的test.php
文件
另一件事是,在将JSON对象发送到服务器时,不需要将其字符串化,jquery
自动处理。只需将obj
作为post数据发送即可
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");
});
我用空文件在我的项目下创建了它我试图在Chrome的网络选项卡上查看,但我只能看到错误文件在我的项目中可以吗?我不知道你说的“我的项目”是什么意思.当我从用户代理运行它时,我没有收到任何错误。在发布此问题之前,您是否尝试直接在浏览器中打开test.php?这似乎是php
问题,而不是jQuery
问题,因为您应该知道如何设置PHP页面的根目录
,然后将这些URI
与$一起使用