$.getJSON() 到 $.ajax()

$.getJSON() to $.ajax()

请问如何将下面的$.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"}]}

我想问一下如何确定声明变量的值(例如 content 的值为 user1)是否与 [ 列表中的值相同=19=] 数组中的键?

以下是我的尝试,您可能会在 $.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 的答案,了解有关如何访问/处理对象的大量信息。

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

        function getID(name){

            $.each(data,function(key,value){    //value = object.value (name)

                $.each(value,function(key1,value1){     //value1 = user name (actual names in array)

                    if(value1 == content){
                        console.log(value.id);

                        var name = value.name;                  
                        $('#nameText').text(name);
                        console.log("Name: " + name);

                        return;
                        }
                    });

                });
            }

        getID(data);
        return false;