weixin_33736832 2015-11-03 15:53 采纳率: 0%
浏览 22

$ .getJSON()到$ .ajax()

I would like to ask how could I convert the following $.getJSON() to $.ajax() please.
I have a set of arrays from var googleApi like this:

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"}]}

I would like to ask how could I identify if the value of a declared variable (eg. content with the value of user1) is the same as a value within the list of name keys in the array?

Below is my attempt and you might find my full code in $.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);
});

Below is my attempt on $.ajax() but I got an error of "TypeError: data.result is undefined";
I have also tried using $(this) to replace data.result but without luck... it would be very nice if someone could identify what have I done wrong please:

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);
});

Merci beaucoup :))) x

  • 写回答

2条回答 默认 最新

  • weixin_33726318 2015-11-03 17:13
    关注

    ...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?

    As per your provided response object you could iterate through it and check the values against your variable content:

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

    Example Fiddle


    As for the second part of your question:

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

    The reason you may be getting your error is because find is expecting a jQuery object, you have received a JSON object back from your endpoint, so using dot notation as above will should work as well:

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

    You can see the answer to this question for a bunch of awesome information on how to access / proccess objects.

    Also note your success callback in your code above is not closed, which will create errors.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog