سؤال

I want to request data from an API. It returns JSON formatted data like {"numberOne":12345,"numberTwo":678}

I tried this Javascript code but it gives me a syntax error because the answer is no JSONP but JSON

$.ajax({
  url: 'https://example.com/api',
  dataType: 'JSONP',
  jsonpCallback: 'callback',
  type: 'GET',
  success: function (data) {
      document.write(data);
  }
});

XMLHttpRequest is not working because the request is made to an other domain.

Maybe you know a simple way (I'm Javascript-Newbie) to get the data from the url. Is it possible to make the script ignoring the syntax of the response?

هل كانت مفيدة؟

المحلول

Change the data type to JSON

dataType: "json"

Read here: http://api.jquery.com/jquery.getjson/

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top