Take the 2-minute tour ×
Stack Apps is a question and answer site for apps, scripts, and development with the Stack Exchange API. It's 100% free, no registration required.

i'm trying getting questions by tags.

this is my simple code and i'm running this from my http://localhost:

<script type="text/javascript">

  $(function(){
  $.ajax({ 
    url:'http://api.stackoverflow.com/1.1/search?tagged=php&page=1&pagesize=15',
    dataType:'jsonp',
    type:'GET',
    error:function(e){
   console.log(e);
    },
    beforeSend:function(){

    },
    success: function(data){
 data = JSON.parse(data);
  $.each(data,function(e){
  $("#wrapper").append(data[e].total);
  });

  }
 });
  });
</script>

then i receive an error in console:

SyntaxError: invalid label

"total": 2000,

search...8184757 (line 2, col 2)

seems the error is generated by the json returned format, cause "total" is a result key.

how can i fix this??

share|improve this question

2 Answers 2

up vote 1 down vote accepted

For one use the current API version (2.1) instead of an old one (1.1 in your case).

Furthermore, when using JSONP you do not have to parse the result. The data object you get in your success method already is a JavaScript object you can work with.

$(function(){
  $.ajax({
    url: 'http://api.stackexchange.com/2.1/search?page=1&pagesize=15&tagged=php&site=stackoverflow',
    dataType:'jsonp',
    type:'GET',
    error:function(e){
      console.log(e);
    },
    success: function(data){
      // do something with the result here    
    }
 });
});
share|improve this answer
    
+1 this work without specifying jsonp in the url –  Badaboooooom Dec 6 '12 at 13:46

The API expects JSONP requests to specify the callback as the jsonp parameter, not the callback that jQuery uses by default.

While really you should consider upgrading to API 2.1, which does expect callback (and allows CORS, for that matter), you can fix your code by putting in the jsonp parameter placeholder:

$.ajax({ 
    url:'http://api.stackoverflow.com/1.1/search?jsonp=?&tagged=php&page=1&pagesize=15',
// ...                                            ^-- this

Also, you don't need to call JSON.parse() on the response data, since it will be an object already.

share|improve this answer
    
+1 tryed but i receive service not available in http response :/ –  Badaboooooom Dec 6 '12 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .