请求带有text / plain MIME Type的JSONP

时间:2014-10-27 20:54:14

标签: javascript jquery ajax json

我正在尝试使用JSONP在另一个域上请求Feed。我知道内容类型应该是JSON或JavaScript,但它是text / plain,我无法控制服务器,因此我无法更改标头。如何让AJAX调用工作?

这是我到目前为止所拥有的 -

function asdf() {
    $.ajax({
        url: "http://example.com/path/to/sharepoint/_vti_bin/listdata.svc/TestCalendar/$count",
        jsonp: "callback",
        dataType: "jsonp",
        contentType: "text/plain",

        // work with the response
        success: function( response ) {
            console.log( response ); // server response
        }
    });
}

如果我只是尝试常规请求,显然我只是收到一个CORS错误。

XMLHttpRequest无法加载http://example.com/path/to/sharepoint/_vti_bin/listdata.svc/TestCalendar/ $ count。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://example.com”访问。响应的HTTP状态代码为401.

1 个答案:

答案 0 :(得分:0)

尝试在浏览器中直接执行您的网址,以确保一切正常 之后,尝试使用此JQuery配置:

$.ajax({
        url        : "YOUR_URL",
        dataType   : 'jsonp',
        crossDomain: true,
        type       : 'POST',
        timeout    : 30000, // in milli seconds
        cache      : false
        success:function(response) {
            console.log(response);
        }
    });