0

I'm new to JavaScript and jQuery, so I'm not even sure this is possible

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. Can I put a for loop inside the query parameter to achieve this? 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
3
  • You can't do that with that code, but you can wrap the whole thing in a loop. Commented Mar 7, 2014 at 2:10
  • That url doesnt seems to be support jsonp Commented Mar 7, 2014 at 2:16
  • @alex how would I wrap the whole thing in a loop? Commented Mar 7, 2014 at 2:22

1 Answer 1

1

You can't use for loop as a value of a object property.

If the api accepts an array to as the query parameter, then just pass the array to it.

        data : {
            api_key : myApiKey,
            query : movies
        }

If it accepts a string with comma splited string, then convert the array to string by join method.

        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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Putting the ajax request inside a for loop was what worked. Just took me a bit to think about how to write that. But it's working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.