外部动态创建的JSON文件提供了Unexpected Token和parsererror

时间:2016-06-29 10:25:23

标签: jquery json ajax

我已经查看了过去24小时内所有可能的选项和解决方案,我仍然坚持这个错误。这是事物,文件是动态创建的(在JAVA中),它是一个外部URL。文件类型为application/json,如下所示:

{
    "main": [{
        "title": "90 days",
        "startDate": "2016-06-16",
        "endDate": "2016-09-14",
        "productName": "Main",
        "product": "main",
        "price": 398,
        "phone": null,
        "type": "Main",
        "campaign": null
    }, {
        "title": "90 days",
        "startDate": "2016-06-16",
        "endDate": "2016-09-14",
        "productName": "Main",
        "product": "main",
        "price": 398,
        "phone": null,
        "type": "Main",
        "campaign": null
    }],
    "extra": [{
        "title": "extra",
        "startDate": "2016-08-14,22:00:00",
        "endDate": "2016-09-12,22:00:00",
        "productName": "extra",
        "product": "extra",
        "price": 29,
        "phone": null,
        "type": "Extra",
        "campaign": ""
    }, {
        "title": "extra",
        "startDate": "2016-07-15,22:00:00",
        "endDate": "2016-08-13,22:00:00",
        "productName": "extra",
        "product": "extra",
        "price": 29,
        "phone": null,
        "type": "Extra",
        "campaign": ""
    }]
}

现在我尝试了几种组合,其中没有一种能够奏效,而且我对以下内容感到困惑。

var myData= {};
$.ajax({
    url: 'http://__external-URL__/product', /* no extension */
    dataType: 'jsonp',
    jsonpCallBack: 'callback',              /* tried function() { alert('foo'); } */
    type: 'GET',
    contentType: "application/json",
    async: false,                           /* tried true as well as totally removing it */
    success: function (data) {
        myData = $.parseJSON(data);
        console.log('success ' + myData);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log('jqXHR: '+ jqXHR);
        console.log('textStatus: '+ textStatus);
        console.log('errorThrown: '+ errorThrown);
    }
});

回复是:

Uncaught SyntaxError: Unexpected token :
jqXHR: [object Object]
textStatus: parsererror
errorThrown: Error: jQuery19005705788836383217_1467193897750 was not called

根据浏览器>检查>网络:

General:
    Request Method:GET
    Status Code:200 OK

and
Response Header:
    Content-Type:application/json
    Server:Apache-Coyote/1.1
    Transfer-Encoding:chunked

我的代码出了什么问题?提前感谢您的解决方案。

如果我将数据类型更改为JSON并删除async: false,我会得到:

  

XMLHttpRequest无法加载 external-URL / product。对预检请求的响应没有通过访问控制检查:否'访问控制 - 允许 - 来源'标头出现在请求的资源上。来源' 外部网址'因此不允许访问。响应的HTTP状态代码为403。

0 个答案:

没有答案