will
will

Reputation: 1511

GitHub API results different between cURL and jQuery AJAX

I'm trying to query GitHub's search user API by language and location. When I use the command curl https://api.github.com/search/users?q=location:denver+language:php I receive 146 results. However, when I try to use jQuery AJAX, I receive an error.

My JavaScript code:

var url = "https://api.github.com/search/users";

var request = {
    q: "location:denver+language:php",
    sort: "repositories",
    order: "desc"
};

var result = $.ajax({
    url: url,
    data: request,
    dataType: 'jsonp',
    type: 'GET'
})
.done(function() {
    alert(JSON.stringify(result));
})
.fail(function() {
    alert('failed');
});

What alert(JSON.stringify(result)); gives:

{
  "readyState":4,
  "responseJSON":{
     "meta":{
        "X-RateLimit-Limit":"10",
        "X-RateLimit-Remaining":"9",
        "X-RateLimit-Reset":"1397393691",
        "X-GitHub-Media-Type":"github.beta",
        "status":422
     },
     "data":{
        "message":"Validation Failed",
        "documentation_url":"https://developer.github.com/v3/search/",
        "errors":[
           {
              "message":"None of the search qualifiers apply to this search type.",
              "resource":"Search",
              "field":"q",
              "code":"invalid"
           }
        ]
     }
  },
  "status":200,
  "statusText":"success"
}

When I only use one qualifier on q it works fine and the result.responseJSON.data object contains the information that is normally provided by cURL.

Upvotes: 1

Views: 519

Answers (1)

keune
keune

Reputation: 5795

use a space character instead of a plus(+). Change your query to this:

q: "location:denver language:php",

and it will work as expected, because jquery will convert this space character to a plus.

Upvotes: 2

Related Questions