jquery ajax响应类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15671679/
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-08-26 15:33:42  来源:igfitidea点击:

jquery ajax response type

jqueryajax

提问by ducin

I'm trying to make a working example of jquery 1.9.1 ajax + icanhaz/mustache. This is my template:

我正在尝试制作 jquery 1.9.1 ajax + icanhaz/mustache 的工作示例。这是我的模板:

<script id="user" type="text/html">
    {{#users}}<li>Username: {{ username }}, fullname: {{ fullname }}</li>{{/users}}
</script>

and this is my javascript:

这是我的javascript:

$(document).ready( function() {
    $("#user-btn").click(function() {
        $.ajax({
            type: "GET",
            url: "../php/client/json.php",
            data: {
                type: "users"
            }
        }).done(function( response ) {
            var element = $('#dialog-message');
            element.html("<ul>");
            element.append(ich.user(response));
            element.append("</ul>");
        });
});

The AJAX response from this address looks something like:

来自该地址的 AJAX 响应类似于:

{"users":[{"username":"jd","fullname":"John Doe"},{"username":"jl","fullname":"John Lennon"}]};

With the following code, icanhaz cannot render anything for me. I spent some time with javascript console and found out that the typeof responseis stringand I expected object. Icanhaz also expects object - that's why it didn't manage to render the correct response.

使用以下代码,icanhaz 无法为我呈现任何内容。我花了一些时间使用 javascript 控制台,发现typeof responsestring和我期望的object. Icanhaz 也期望 object - 这就是为什么它没有设法呈现正确的响应。

Am I doing something wrong or am I just a poor newbie who didn't know that jquery.ajax returns string responses always? If so, how should I handle them?

我做错了什么还是我只是一个不知道 jquery.ajax 总是返回字符串响应的可怜的新手?如果是这样,我应该如何处理它们?

回答by Steve

If you are getting a stringreturned from your AJAX call, you need to add dataType: "json". This will make jQuery parse the response as JSON, if possible.

如果您string从 AJAX 调用中得到返回,则需要添加dataType: "json". 如果可能,这将使 jQuery 将响应解析为 JSON。

$(document).ready( function() {
    $("#user-btn").click(function() {
        $.ajax({
            type: "GET",
            url: "../php/client/json.php",
            data: {
                type: "users"
            },
            dataType: "json"
        }).done(function( response ) {
            ...
        });
});

Are you sure your ich.usermethod expects an array of users and not just a single user object?

您确定您的ich.user方法需要一组用户而不是单个用户对象吗?

回答by Marcel Gwerder

Try the option dataType: "json"for $.ajax.

请尝试选择dataType: "json"$.ajax

回答by pala?н

Modify the click function:

修改点击功能:

$("#user-btn").click(function () {
    $.ajax({
        type: "GET",
        url: "../php/client/json.php",
        data: {
            type: "users"
        },
        dataType: 'json',
        success: function (data) {
            // Your code...
        },
        error: function (xhr, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
});