正确使用JSONP

Correct use of JSONP

要成功使用 JSONP(例如通过 jquery - $.ajax ... 等)必须始终要求请求的页面旨在提供与此格式对应的数据?

换句话说,如果我对纯静态内容(即没有php、aspx等)的页面执行请求,也会出错吗?

这个问题对某些用户来说可能是微不足道的,但我现在才开始学习这些技术,问题有点复杂。

基于这些 (ref1 ref2) 参考,使用 JSONP 的请求与服务器响应的实现之间似乎必须保持一致。

编辑

我有这个 jQuery 请求

$.ajax({
    url: "https://sites.google.com/site/problemsstore/javascript/test.js",
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    dataCharset: 'jsonp',
    success: function (result) {
        console.log('request succeed');
    },
    error: function (result) {
        console.log('failed');
    }
});

并且我已经在 https://sites.google.com/site/mysite/javascript/test.js?attredirects=0&d=1 中加载了这个 test.js 文件:

function myCall(data) {
console.log('succeed');
}
myCall({ some : "data" });

连接后我希望获得控制台输出:succeed succeed.

相反,这是我得到的:

succeed 
failed

编辑2

$.ajax({
    url: "https://sites.google.com/site/bentofelicianolopez/jscript-jsonp/test.js?attredirects=0&d=1",
    type: 'GET',
    crossDomain: true,
    dataType: 'jsonp',
    dataCharset: 'jsonp',
    jsonp: 'myCall',
    //contentType: 'application/json',
    success: function (result) {
        console.log('request succeed');
    },
    error: function (result) {
        console.log('failed');
    }
});

.js 文件:

myCall({ some : "data" });

输出:

failed test4.html:94:9
ReferenceError: myCall is not defined /*this is the syntactical error of which I said*/
 test.js:1:1

To successfully use JSONP (e.g. via jquery - $ .ajax ... etc.) must always be, that the requested page is designed to provide data corresponding to this format?

是的。仅当响应表示为 JSONP 时,对 JSONP 的请求才有效。

In other words, if I perform a request to a page with a pure static content (i.e. no php, aspx, and so on), also I will get an error?

你可以有一个符合JSONP格式的静态JavaScript程序(它需要硬编码回调函数名称),但不一定。