Difficulty with Ajax Jsonp

Asked

Viewed 66 times

0

I’m trying to consume an api through Ajax, but I get error while running it.

var data2 = {
    resource_id: '1d7e45e5-b159-46a2-bdce-90393c7a8a2b', // the resource id
    limit: 5, // get 5 results
    q: 'jones', // query for 'jones'      
};
$.ajax({
    url: 'https://demo.ckan.org/api/3/action/datastore_search',
    data: data2,       
    dataType: 'jsonp',
    success: function (data2)
    {
        debugger;
        alert('Total results found: ' + data2.result.total)
    }
});

In the Browser Console I noticed that it adds two parameters that I did not pass :callback=jQuery21409543886738626624_1528899809074 e &_=1528899809075

URL that appears in the console:

https://demo.ckan.org/api/3/action/datastore_search?callback=jQuery21409543886738626624_1528899809074&resource_id=1d7e45e5-b159-46a2-bdce-90393c7a8a2b&limit=5&q=jones&_=1528899809075

As it should be:

https://demo.ckan.org/api/3/action/datastore_search?resource_id=1d7e45e5-b159-46a2-bdce-90393c7a8a2b&limit=5&q=jones 

1 answer

0


In your Ajax include the cache: true see here why

In Browser Console I noticed that it adds two parameters that I did not pass :callback=jQuery21409543886738626624_1528899809074 and &_=1528899809075

var data2 = {
  resource_id: '1d7e45e5-b159-46a2-bdce-90393c7a8a2b', 
  limit: 5,
  q: 'jones',    
};
$.ajax({
  url: 'https://demo.ckan.org/api/3/action/datastore_search',
  data: data2,
  dataType: 'jsonp',
  cache: true,
  success: function(result) {
    alert('Resultados encontrados: ' + result.result.total)
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

  • You solved my problem. Thank you very much !

  • Thank you. If possible vote as an answer. Att.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.