jQuery Ajax无法解析json吗?

我遇到了一个非常奇怪的问题,以前以为有用的但现在不行了。

我在尝试使用旧的jQuery库时看到一条错误消息: http://i.imgur.com/H51wG4G.png,第68行:var jsondata = $ .parseJSON(data);。

我的ajax函数由于此错误,也无法使警报正常工作。 顺便说一下,该脚本是用于登录的,因此,如果我刷新网站,则会登录。 正如在图像中看到的,我还很好地返回了我的json对象:"success":false,"msg":"Fel anv\u00e4ndarnamn eller l\u00f6senord.","redirect":""}

收到此消息后,如果获得成功== true,我将登录login.success并从login-in.php获取登录面板。

$('#login_form').submit(function()
{
    var login = $.ajax(
    {
        url: '/dev/ajax/trylogin.php',
        data: $(this).serialize(),
        type: 'POST',
    }, 'json');
    login.success(function(data)
    {
        var jsondata = $.parseJSON(data);
        console.log(jsondata);
        if(jsondata.success == true)
        {
            $.get("/dev/class/UI/logged-in.php", function(data) {
                $(".login-form").replaceWith(data);
            });
        }
        else
        {
            alert(jsondata.msg);
            $('#pwd').val('');
        }
    });
    return false;
});

多谢!

Your response is not a valid JSON. You see: "unexpected token <".

It means that your response contains an unexpected "<" and it cannot be converted into JSON format.

Put a console.log(data) before converting it into JSON.

If the response you have showed in the attached screenshot is something to go by, you have a problem in your PHP script that's generating the JSON response. Make sure that thePHP script that's generating this response (or any other script included in that file) is not using a constant named SITE_TITLE. If any of those PHP files need to use that constant, make sure that that SITE_TILE is defined somewhere and included in those files.

What might have happened is that one of the PHP files involved in the JSON response generation might have changed somehow and started using the SITE_TITLE costant without defining it first, or without including the file that contains that constant.

Or, maybe none of the files involved in the JSON generation have changed, but rather, your error_reporting settings might have changed and now that PHP interpreter is outputting the notice level texts when it sees some undefined constant.

Solving the problem

If the SITE_TITLE constant is undefined, define it. If the SITE_TITLE constant is defined in some other file, include that file in the PHP script that's generating the response.

Otherwise, and I am not recommending this, set up your error_reporting settings to ignore the Notice.

You shoud use login.done() , not login.success() :) Success is used inside the ajax() funciton only! The success object function is deprecated, you can set success only as Ajax() param!

And there is no need to Parse the data because its in Json format already!

jQuery Ajax

$('#login_form').submit(function()
{
    var login = $.ajax(
    {
        url: '/dev/ajax/trylogin.php',
        data: $(this).serialize(),
        type: 'POST',
    }, 'json');
    login.done(function(data)
    {
        var jsondata = data;
        console.log(jsondata);
        if(jsondata.success == true)
        {
            $.get("/dev/class/UI/logged-in.php", function(data) {
                $(".login-form").replaceWith(data);
            });
        }
        else
        {
            alert(jsondata.msg);
            $('#pwd').val('');
        }
    });
    return false;
});