Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery中的post问题_Javascript_Jquery - Fatal编程技术网

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
$一起使用