문제

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