0

My current code is :

$.getJSON("https://www.domain.com/someapi/callback=?",
    function(data){
      $.each(data, function(i,item){            
        alert(item.x);
      });
  });

Right now I'm getting an error because they're adding a line to the top of my json response. is there a way to get rid of that line?

Thanks!

UPDATE:

I have also tried doing something like this:

$.ajax({
    url: "https://www.domain.com/someapi/callback=?",
    type: "get",
    success: function (data) {
        alert(data)
    }
});

but it's a crossdomain call, so i get that error.

Peter Gluck
  • 8,168
  • 1
  • 38
  • 37
tehras
  • 741
  • 1
  • 7
  • 19
  • $.getJSON need a valid json. You have to make a normal request, get the text, cut the first line and then parse it to json. – GramThanos Aug 08 '14 at 01:05
  • Ok, but how do i make a cross domain request with normal? See my UPDATE. – tehras Aug 08 '14 at 01:07
  • What is that line? What does the server return? – Ram Aug 08 '14 at 01:07
  • 'allowIllegalResourceCall is false.' it's to protect the api from illegal stuff :) – tehras Aug 08 '14 at 01:09
  • set `crossDomain` to true and `dataType` to `jsonp` along with the other properties when making your ajax call. Also see this: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain – Gad Aug 08 '14 at 01:12
  • _“it's to protect the api from illegal stuff”_ – stuff like what you are trying to do …? – CBroe Aug 08 '14 at 01:15
  • look here: http://stackoverflow.com/questions/3506208/jquery-ajax-cross-domain , for the crossDomain error. You can always set the dataType to "jsonp" to fix it. – ZekeDroid Aug 08 '14 at 01:15
  • I cannot use jsonp, because the first line isn't a json type.. – tehras Aug 08 '14 at 01:17
  • And no, i'm not trying to do illegal stuff, I can do this on the server side, but i don't want my server to get any more hits, so i need to contact this api directly from ajax. – tehras Aug 08 '14 at 01:18

1 Answers1

0

Make a ajax normal request

$.ajax({
    url : "https://www.domain.com/someapi/callback=?",
    success : function(result){
        // So here we get the result json with an error
        // So lets say the response is something like this
        /*
            There was an error on line 35
            {
                json : true
            }
        */
        // So we remove everything before the first '{'
        result = result.replace(/[^{]*/i,'');
        //We parse the json
        var data = JSON.parse(result);
        // And continue like no error ever happened
        $.each(data, function(i,item){            
            alert(item.x);
        });
    }
});

I hope this work. (a cross domain request must be enabled from the server)

GramThanos
  • 3,572
  • 1
  • 22
  • 34
  • I can't enable the cross domain request from the server side. – tehras Aug 08 '14 at 01:19
  • If you was able to make a $.getJSON with not cross domain error this is already enabled. If it is not disabled it is probably in order not to make cross domain requests. Its a security issue. – GramThanos Aug 08 '14 at 01:22
  • I can make calls using jsonp or .getJSON with callback, but they put a line in the json to make sure there aren't any automatic crawlers or phishers that are out there looking for data. – tehras Aug 08 '14 at 01:24
  • If the getJSON works so will probably the code i gave you. But keep in mind that you try to 'crawl', and thats what they try to prevent. – GramThanos Aug 08 '14 at 01:37