Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.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
Jquery 没有从ajax$.post()返回的数据_Jquery_Ajax_Json_Post - Fatal编程技术网

Jquery 没有从ajax$.post()返回的数据

Jquery 没有从ajax$.post()返回的数据,jquery,ajax,json,post,Jquery,Ajax,Json,Post,从我的jQuery AJAX帖子中获取任何响应文本时遇到问题: $('document').ready(function() { $('#saveRouteTrace').click(function(){ // Build latStr and lonStr if(saveChanges()) { $.post('changeRouteTrace.php', { id: trac

从我的jQuery AJAX帖子中获取任何响应文本时遇到问题:

$('document').ready(function()
{

    $('#saveRouteTrace').click(function(){

        // Build latStr and lonStr
        if(saveChanges()) {
            $.post('changeRouteTrace.php', {    
                    id: traceRouteId 
                   },
            function(data) {
                if(data.success) {
                    alert(data.reason);
                }
                else {
                    alert("Error: " + data.reason); 
                }                   
            }, 'json');
        }
    });
});
我的回答如下:

{ "success": true, "reason": "because it worked" }

来自服务器,但函数(数据)代码从不执行。我做错了什么?

您可以使用FireBug检查请求是否已发送,响应是否正常。如果发送了请求,但没有得到正确的响应,那么用HTML创建一个测试表单并使用该表单发布数据可能会更容易。调试这比使用javascript响应更容易,因为您可以在更改后保持刷新并在浏览器中检查响应。

假设savechanges()函数返回一个值,此代码快照在我的系统中工作正常

$(document).ready(function(){

    $('#saveRouteTrace').click(function(){

        // Build latStr and lonStr
        if(saveChanges()) {
            $.post('stackoverflowajax.php', {    
                    id: 9,
                    name:'Tsegay' 
                   },
            function(data) {
                    alert(data);
                if(data.success) {
                    alert(data.reason);
                }
                else {
                    alert("Error: " + data.reason); 
                }                  
            }, 'json');
        }
    });
        function saveChanges(){
            //This is a debuging code ... Make sure it return a value
                return true;
            }
    });
这是服务器端的文件。我正在回显json文件

if(isset($_POST['id'])){
            //This do not work
            return '{ "success": true, "reason": "because it worked" }';
        //this works fine
        echo '{ "success": true, "reason": "because it worked" }';
    }

很难说,代码看起来不错。您确定已发送请求吗?
saveChanges()
是否返回
true
?如何创建响应?在您最喜欢的调试工具的“网络”选项卡中可以看到什么?你需要添加更多的信息。你能检查一下我将要向服务器发送数据的脚本吗?你正在返回
{“success”:true,“reason”:“因为它工作”}
,或者你认为/想返回这个JSON(两者之间有区别)?我知道答案。请询问他。在您的doc ready下添加.ajaxError$('body').ajaxError(函数(){$(this.append('Triggered ajaxError handler.');});我确信请求已发送,因为服务器端正在发生更改。我回应{“success”:true,“reason”:“bc it worked”},这就是我需要做的全部吗?我检查了Chrome调试器,从changeRouteTrace.php获得了预期的响应,但从我的函数(data){}函数中什么都没有。有什么想法吗?