javascript - $.getJSON() 到 $.ajax()

标签 javascript jquery ajax getjson

我想问一下如何将下面的 $.getJSON() 转换为 $.ajax()
我有一组来自 var googleApi 的数组,如下所示:

Array [Object, Object, Object, Object, Object]

// if stringified
[{"id":"0","name":"user1","type":"mf","message":"bonjour user1"},
{"id":"1","name":"user2","type":"ff","message":"hello user2"},
{"id":"2","name":"user3","type":"mm","message":"konnichiwa user3"},
{"id":"3","name":"user4","type":"mf","message":"ni hao user4"},
{"id":"4","name":"user5","type":"ff","message":"high 5! user5"}]}

我想问一下如何确定声明的变量的值(例如,值为user1content)是否与内的值相同数组中 name 键的列表?

下面是我的尝试,您可能会在 $.getJSON() here 中找到我的完整代码:

$.getJSON():

var googleApi = 'https://api.com/url_here';

$.getJSON(googleApi, function(json){

    console.log(JSON.stringify(json));
    var item = json.result.find(function(e){

    return e.name == content;

    }) || json.result[0];           
    console.log("PRINT ID: " + item.id);

    var name = item.name || content;                    
    $('#nameText').text(name);
    console.log("Name: " + name);
});

下面是我对 $.ajax() 的尝试,但收到错误“TypeError: data.result is undefined”;
我也尝试过使用 $( this) 来替换 data.result 但运气不好...如果有人能指出我做错了什么,那就太好了:

var googleApi = "https://sheetsu.com/apis/v1.0/f924526c";
var googleKey = "0123456789";
var googleSecret = "987654321";

var data = [];
$.ajax({
    url: googleApi,
    headers: {
    "Authorization": "Basic " + btoa(googleKey + ":" + googleSecret)
    },
    data: JSON.stringify(data),
    dataType: 'json',
    type: 'GET',

    success: function(data) {

        console.log(data);                      

        var item = data.result.find(function(e){

            return e.name == content;

        }) || data.result[0];           
        console.log("PRINT ID: " + item.id);

        var name = item.name || content;                    
        $('#nameText').text(name);
        console.log("Name: " + name);
});

谢谢你:))) x

最佳答案

...how could I identify if the value of a declared variable ... is the same as a value within the list of name keys in the array?

根据您提供的响应对象,您可以 iterate through it并根据变量content检查值:

var content = "user1";

$.each(response, function(i, v) {
   if (v.name == content) {
      console.log(v.name);
   }
});

Example Fiddle

<小时/>

至于你问题的第二部分:

but I got an error of "TypeError: data.result is undefined";

您收到错误的原因可能是 find需要一个 jQuery 对象,您已从端点收到一个 JSON 对象,因此使用上面的点表示法也应该有效:

success: function(data) {
    $.each(data, function(i, v) {
        if (v.name == content) {
            console.log(v.name);
        }
    });

}

您可以看到this question的答案有关如何访问/处理对象的大量精彩信息。

另请注意,上面代码中的成功回调未关闭,这会产生错误。

关于javascript - $.getJSON() 到 $.ajax(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33503484/

相关文章:

javascript - UI GRID - 自定义单元格模板代码分离

jquery - css( "background-color", "")不工作

javascript - 循环内异步函数的调用层次结构?

javascript - 我无法使用 map 功能 TypeError : Cannot read property 'map' of undefined

javascript - 表单操作值未从 attr() 函数获取

javascript - 将 D3 力图转换为 v4

javascript - 简单的链表遍历问题

javascript - 确定设备方向(具体情况)

带有 async=true 的 JQuery .ajax 总是返回错误

javascript - 从生成的表中获取值