3

I've been googling this, found recommendations but I couldn't apply correctly. I am calling a json API from other domain so i used jsonp. Please check my code.

$(document).ready(function () {               
    $("#btnSubmit").click(function () {
        GetParam(function (data) {
            alert(data.toString());
        });
    });
 });

  function GetParam(func) {
      $.getJSON("http://test.com/api/getparams.ashx?callback=?", function (result) {});
  }

It is able to call the API and show the result. It's just that there is this semicolon issue.

enter image description here

From what i have researched, they say i should return a jsonp format but how can i do that? Note: I don't have access to the source code of the API.

Thanks.

Liz
  • 323
  • 7
  • 17
  • 2
    If you cannot make the API return JSONP, you cannot use JSONP. It's as simple as that. JSONP is not a silver bullet for cross-domain connections, it actually has to be supported by the server. – Felix Kling Dec 03 '13 at 15:38
  • the server endpoint does not seem to be returning jsonp data. If this feature is not available, you might consider using `CORS` instead – jbl Dec 03 '13 at 15:38
  • 2
    possible duplicate of [AJAX call and clean JSON but Syntax Error: missing ; before statement](http://stackoverflow.com/questions/19456146/ajax-call-and-clean-json-but-syntax-error-missing-before-statement) – Felix Kling Dec 03 '13 at 15:40
  • Do you control the domain that is sending the data? – Explosion Pills Dec 03 '13 at 15:40
  • Are you sure the API supports JSONP in the first place? Adding `callback=?` doesn't magically make it work, the API needs to support it first. Check its docs. – gen_Eric Dec 04 '13 at 14:56
  • 1
    thanks. it is now clear to me that the API should support jsonp first. To fix this easier and faster, I have asked the owner of the API to add crossdomain.xml and allow request from my domain :) then I am now using the normal json. – Liz Dec 05 '13 at 04:37

1 Answers1

0

If you use jsonp use $.ajax({dataType: 'jsonp'...});

Stedy
  • 7,359
  • 14
  • 57
  • 77
jesusnoseq
  • 341
  • 4
  • 9