Cremix_hellven
Cremix_hellven

Reputation: 995

How does this function know which attribute is being passed?

I'm checking a little app what gets you a list of movies from an API. You enter a word and then it list you all movies with that word. The code that gets you the list is this one:

var requestApi = function (query, callback) {                                   
    $.ajax({
            url: apiUrl,
            data: {
            q: query},
            dataType: 'jsonp'
            }).success(callback);
};

When the code calls this function, it does:

requestApi(movieTitle, callbackFunction);

And this is a piece of the JSON file:

{
    "total": 33,
    "movies": [
        {
            "id": "10122",
            "title": "Spider-Man",
            "year": 2002,
             ...
}

My question is, how does requestApi know that q: query is the title? Why doesn't work if I call requestApi(movieId, callbackFunction); I don't understand it.

Thanks

Upvotes: 0

Views: 71

Answers (5)

xec
xec

Reputation: 18034

After a quick look at http://developer.rottentomatoes.com/io-docs it seems the URLs for looking up a movie by ID or Title are different:

For title search you use parameter q at url:

/api/public/v1.0/movies.json

By ID you can use the ID as part of the url (replace :id with the id number):

/api/public/v1.0/movies/:id.json

Upvotes: 0

Sruly
Sruly

Reputation: 10550

You should probably look into the Api documentation, q must be the parameter for text search. If you want to search by id you will probably need to supplement q with another variable name.

If you look at the Imdb api you use "s" for title search and "i" for id seach. http://www.omdbapi.com/

Upvotes: 2

Icepick
Icepick

Reputation: 156

Because the API clearly looks for that word movie titles in database. Same as if you were searching the movie by hand lets say on IMDB. You would get no results if you searched an ID of that movie in their DB

Upvotes: 1

Danny Hoek
Danny Hoek

Reputation: 420

The API endpoint you're sending the request to probably only handles searching in the title node. What API are you using?

Upvotes: 0

gorgi93
gorgi93

Reputation: 2535

Api you provide in URL is accepting movie title as a parameter. It is probably rest like api. It is assuming you will send title as parameter, you should read the API documentation to see what to send as parameter

Upvotes: 1

Related Questions