同时加入JSON响应到一个数组错误 [英] Error while adding JSON responses to an array

查看:115
本文介绍了同时加入JSON响应到一个数组错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用goog.gl API一个URL缩短。感谢@Barmar现在我可以用这个code得到我的短网址:

  VAR SHORTURL;
    $阿贾克斯({
        网址: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        输入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:{longUrl:'+ longURL +'}
        数据类型:JSON,
        成功:函数(响应){
            SHORTURL = response.id;
        }
    });

但我想缩短链接一个数组中!所以,我决定使用循环。

我创建longURL []和SHORTURL [],但如果我运行此code我得到SHORTURL数组,输出: [未定义×10,http://goo.gl/AxzWLx ]; 全code:

  VAR longURL = []; //还有一些网址
VAR SHORTURL = [];
对于(VAR K = 0; K< longURL.length; k ++){
    $阿贾克斯({
        网址: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        输入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:{longUrl:'+ longURL [K] +'}
        数据类型:JSON,
        成功:函数(响应){
            SHORTURL [K] = response.id;
        }
    });
}


解决方案

的问题是,所有的回调函数共享 K ,同样的价值,因为它不是一个每 - 函数闭包变量。您可以使用背景:选项为适当的值传递给每个回调

  VAR longURL = []; //还有一些网址
VAR SHORTURL = [];
对于(VAR K = 0; K< longURL.length; k ++){
    $阿贾克斯({
        网址: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        输入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:{longUrl:'+ longURL [K] +'}
        数据类型:JSON,
        背景:K,
        成功:函数(响应){
            SHORTURL [这] = response.id;
        }
    });
}

另一种解决方案是使用 $。每个()。由于每个迭代函数调用,你会收过来的参数:

  VAR longURL = []; //还有一些网址
VAR SHORTURL = [];
$。每个(longURL,功能(K,URL){
    $阿贾克斯({
        网址: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        输入:POST,
        的contentType:应用/ JSON的;字符集= UTF-8,
        数据:{longUrl:'+ URL +'}
        数据类型:JSON,
        成功:函数(响应){
            SHORTURL [K] = response.id;
        }
    });
});

I am trying to make a url shortener using goog.gl api. Thanks to @Barmar now I can get my short URL using this code:

var shortURL;
    $.ajax({
        url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: '{ longUrl: "' + longURL +'"}',
        dataType: 'json',
        success: function(response) {
            shortURL = response.id;
        }
    });

But I want to shorten an array of links! So I decided to use loop.

I created longURL[] and shortURL[] but if I run this code I get such output in shortURL array: [undefined × 10, "http://goo.gl/AxzWLx"]; Full code:

    var longURL = [];//there are some urls
var shortURL = [];
for (var k = 0; k < longURL.length; k++) {
    $.ajax({
        url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: '{ longUrl: "' + longURL[k] +'"}',
        dataType: 'json',
        success: function(response) {
            shortURL[k] = response.id;
        }
    });
}

解决方案

The problem is that all your callback functions share the same value of k, because it's not a per-function closure variable. You can use the context: option to pass the appropriate value to each callback.

var longURL = [];//there are some urls
var shortURL = [];
for (var k = 0; k < longURL.length; k++) {
    $.ajax({
        url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: '{ longUrl: "' + longURL[k] +'"}',
        dataType: 'json',
        context: k,
        success: function(response) {
            shortURL[this] = response.id;
        }
    });
}

Another solution is to use $.each(). Since each iteration is a function call, you'll close over the parameters:

var longURL = [];//there are some urls
var shortURL = [];
$.each(longURL, function(k, url) {
    $.ajax({
        url: 'https://www.googleapis.com/urlshortener/v1/url?shortUrl=http://goo.gl/fbsS&key=AIzaSyANFw1rVq_vnIzT4vVOwIw3fF1qHXV7Mjw',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: '{ longUrl: "' + url +'"}',
        dataType: 'json',
        success: function(response) {
            shortURL[k] = response.id;
        }
    });
});

这篇关于同时加入JSON响应到一个数组错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆