简体   繁体   English

在jQuery AJAX请求中使用for循环

[英]Using a for loop in a jQuery AJAX request

I'm new to JavaScript and jQuery, so I'm not even sure this is possible 我是JavaScript和jQuery的新手,所以我甚至不确定这是否可行

I'm trying to run an AJAX request in which a search runs through an array of titles so that I can then later store other information that is returned for later use. 我试图运行一个AJAX请求,其中搜索通过一系列标题运行,以便以后可以存储返回的其他信息供以后使用。 Can I put a for loop inside the query parameter to achieve this? 我可以在查询参数中放入for循环来实现吗? My code is below but it's not returning anything right now. 我的代码在下面,但现在不返回任何内容。

$.ajax('http://api.themoviedb.org/3/search/movie', {
    type: 'GET',
    dataType: 'jsonp',
    data: {
        api_key: myApiKey,
        query: for (var i = 0; i < movies.length; i++) {
            console.log(movies[i]);
        },
        success: function (result) {
            console.log(result);
        }
    }); // end search ajax request

You can't use for loop as a value of a object property. 您不能将for循环用作对象属性的值。

If the api accepts an array to as the query parameter, then just pass the array to it. 如果api接受一个数组作为查询参数,则只需将数组传递给它。

        data : {
            api_key : myApiKey,
            query : movies
        }

If it accepts a string with comma splited string, then convert the array to string by join method. 如果它接受以逗号分隔的字符串,则可以通过join方法将数组转换为字符串。

        data : {
            api_key : myApiKey,
            query : movies.join()
        }

And if the api doesn't support multiple move search for one query, you have to make a ajax request inside a loop. 如果api不支持对一个查询的多次移动搜索,则必须在循环内发出ajax请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM