跨域请求后返回未捕获的SyntaxError:意外的标记:错误标记、意外、错误、SyntaxError

2023-09-10 14:37:54 作者:吧唧妳壹口

我想获得一个在远程服务器上创建的JSON对象,并在页面上拉出。我曾试图与数据类型,类型,回调等每一个组合,但继续得到同样的意外的标记:错误

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.

这是我曾尝试code:

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:

未捕获的SyntaxError:意外的标记: HTTP: //someurl.com/api/count?url=http://www.yyets.com&_xx1358645456540=1

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);

       }

这一次两罚全中,

未捕获的SyntaxError:意外的标记:数:1   错误... parsererror错误:jQuery19005587419604416937_1358646210172不叫

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

您可以请帮助!

推荐答案

而被JavaScript必须有一个标题叫做页

The page which called by javascript have to have that header

header('Access-Control-Allow-Origin: *');