cross domain request returning Uncaught SyntaxErro

2019-07-23 06:36发布

I am trying to get the JSON object that is created in a remote server and pull it on the page. I have tried every combination with "data type", "type", callback etc but keep getting same "unexpected token :" error.

This is the code that I have tried:

$(function(){
    var url = "http://someurl.com/api/count?url=http://www.yyets.com";
    $.jsonp({
        url: url,
        corsSupport: true, // if URL above supports CORS (optional)
        jsonpSupport: true, // if URL above supports JSONP (optional)
        jsonp:'jsonp',
        dataType: 'jsonp',
        success: function(data, status){
            alert("success");
            console.log(data);
        },
        complete: function(data){
          alert('Completed.');
          console.log(data);
        },
        error: function(XHR, textStatus, errorThrown){
            alert("ERREUR: " + textStatus);
            alert("ERREUR: " + errorThrown);
        }
    });
});

On the console the unexpected token error points to the url:

Uncaught SyntaxError: Unexpected token : http://someurl.com/api/count?url=http://www.yyets.com&_xx1358645456540=1

I also did some error handling with:

error: function (jqXHR, textStatus, errorThrown) {
             if (window.console) console.log("Error... " + textStatus + "        " + errorThrown);

       }

This time it throws,

Uncaught SyntaxError: Unexpected token : count:1
Error... parsererror Error: jQuery19005587419604416937_1358646210172 was not called

Can you please help!!

1条回答
Root(大扎)
2楼-- · 2019-07-23 07:12

The page which called by javascript have to have that header

header('Access-Control-Allow-Origin: *');
查看更多
登录 后发表回答