javascript jQuery post 不返回响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18121540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:47:36  来源:igfitidea点击:

jQuery post returns no response data

javascriptjqueryajaxpost

提问by Avdept

I have this code

我有这个代码

$(document).delegate('#login', 'pageinit', function(event) {

console.log('inside login page')
$('#loginform').submit(function() {

    // Get the value of the username and password
    var myusername = $("#username").val();
    var mypassword = $("#password").val();

    // Post to the login route
    $.post(global_urlstub + '/customer_login', {username: myusername, password: mypassword}, function(data) {

        alert(data);
        console.log(data);
        if (data.flag == true) {
            alert('123');
            console.log(data);
            jQuery.mobile.changePage('#page1');
        }
        else {
            alert('12345');
            console.log(data);
            $('#errmsg_login').html(data.msg);
        }

    }, "json" );

    return false;

});

});

});

My server returns hash with key flag. However this code returns me no data to console or alert , while post request is successful. What am i doing wrong?

我的服务器返回带有 key 的哈希值flag。但是,当 post 请求成功时,此代码不会向我返回任何数据到 console 或 alert 。我究竟做错了什么?

采纳答案by Barrie

I would agree with Hemant_Negi that this is a problem with you server not returning valid JSON. The following code based on yours works fine (change of URL in there too which returns valid JSON):

我同意 Hemant_Negi 的观点,这是您的服务器未返回有效 JSON 的问题。以下基于您的代码工作正常(更改其中的 URL 也返回有效的 JSON):

var myusername = 'a', mypassword = 'b';
$.post('http://ip.jsontest.com', {username: myusername, password: mypassword}, function(data) {
    alert(data.ip);
    console.log(data);
    if (data.flag == true) {
        alert('123');
        console.log(data);
        jQuery.mobile.changePage('#page1');
    }
    else {
        alert('12345');
        console.log(data);
        $('#errmsg_login').html(data.msg);
    }
}, "json" ).fail( function(jqXHR, textStatus, errorThrown) {
    alert(textStatus);
});

If you change that URL, the call to fail() should alert an error message.

如果您更改了该 URL,则对 fail() 的调用应该会发出一条错误消息。

回答by Hemant_Negi

It happens when you server is not returning a valid JSON

当您的服务器未返回有效的 JSON 时会发生这种情况

try it without datatype first like below..

首先尝试没有数据类型,如下所示..

$.post(global_urlstub + '/customer_login', {username: myusername, password: mypassword}, function(data) {

            alert(data);
            console.log(data);
            if (data.flag == true) {
                alert('123');
                console.log(data);
                jQuery.mobile.changePage('#page1');
            }
            else {
                alert('12345');
                console.log(data);
                $('#errmsg_login').html(data.msg);
            }

        });

回答by Jahmic

Make sure that you take any debugging output code in your server side script.

确保在服务器端脚本中使用任何调试输出代码。

Also, using Firebug or chrome tools, you can easily check out the response from the server to see if it is in valid JSON format.

此外,使用 Firebug 或 chrome 工具,您可以轻松检查来自服务器的响应以查看它是否采用有效的 JSON 格式。