如何解析来自serialize()的错误和状态消息?

发布于 2024-11-06 10:53:48 字数 1849 浏览 0 评论 0原文

我有这段代码

$(document).ready(function(){

    //  ...

    $('form').submit(function() {

    // ...

    $.ajax({
        type: "GET",
        url: "/cgi-bin/ajax.pl",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        data: $(this).serialize(),

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $('div#create_result').text(
            "responseText: " + XMLHttpRequest.responseText +
            ", textStatus: " + textStatus +
            ", errorThrown: " + errorThrown);


        $('div#create_result').addClass("error");
        }, // error 

        success: function(result){
        if (result.error) { // script returned error
            $('div#create_result').text("result.error: " + result.error);
            $('div#create_result').addClass("error");
        } // if
        else { // perl script says everything is okay
            $('div#create_result').text(
            "result.success: " + result.success +
            ", result.userid: " + result.userid);
            $('div#create_result').addClass("success");
        } //else
        } // success
    }); // ajax

    $('div#create_result').fadeIn();
    return false;

    });
});

,如果成功的话,它总是给出错误消息。

示例:

responseText: Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}, textStatus: parsererror, errorThrown: SyntaxError: JSON.parse

有什么想法是错的吗?

更新

以下是我在服务器端 Perl 脚本中制作 JSON 字符串的方法。

...
$json = qq{{"error" : "Owner $owner doesn't exist"}};

...
$json = qq{{"success" : "Activity created", "Activity number" : "$id"}};
...

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

I have this code

$(document).ready(function(){

    //  ...

    $('form').submit(function() {

    // ...

    $.ajax({
        type: "GET",
        url: "/cgi-bin/ajax.pl",
        contentType: "application/json; charset=utf-8",
        dataType: "json",

        data: $(this).serialize(),

        error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $('div#create_result').text(
            "responseText: " + XMLHttpRequest.responseText +
            ", textStatus: " + textStatus +
            ", errorThrown: " + errorThrown);


        $('div#create_result').addClass("error");
        }, // error 

        success: function(result){
        if (result.error) { // script returned error
            $('div#create_result').text("result.error: " + result.error);
            $('div#create_result').addClass("error");
        } // if
        else { // perl script says everything is okay
            $('div#create_result').text(
            "result.success: " + result.success +
            ", result.userid: " + result.userid);
            $('div#create_result').addClass("success");
        } //else
        } // success
    }); // ajax

    $('div#create_result').fadeIn();
    return false;

    });
});

which always gives error messages, if it was successful.

Example:

responseText: Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}, textStatus: parsererror, errorThrown: SyntaxError: JSON.parse

Any ideas what is wrong?

Update:

Here is how I make the JSON strings in the server-side Perl script.

...
$json = qq{{"error" : "Owner $owner doesn't exist"}};

...
$json = qq{{"success" : "Activity created", "Activity number" : "$id"}};
...

print $cgi->header(-type => "application/json", -charset => "utf-8");
print $json;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-11-13 10:53:48

这:

parseError,
SyntaxError: JSON.parse

解析器不接受您的响应 JSON,它可能格式错误。您发布的 responseText 中包含标题,这些标题不应出现。:

Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}

This:

parseError,
SyntaxError: JSON.parse

Your response JSON is not acceptable to the parser, it might be malformed. Your posted responseText has headers in it, those should NOT be present.:

Content-Type: application/json; charset=utf-8 {"success" : "Activity created", "Activity number" : "38"}
清眉祭 2024-11-13 10:53:48

考虑使用 Perl 的 JSON 模块,它为您处理序列化和解析。有一个 XS 后端(为了速度),以及一个 PP(纯 Perl)后端。

Consider using Perl's JSON module, which handles serialization and parsing for you. There is an XS backend (for speed), as well as a PP (Pure Perl) backend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文