Tag: jsonp

Chrome扩展中的JSONP请求,回调函数不存在?

我在chrome扩展(内容脚本)中发出JSONP请求。 当我作为网页运行 – 在我的浏览器中加载HTML文件时,一切都运行良好,但是当我将其作为chrome扩展加载时,jquery创建的jsonp回调函数在服务器给出响应时似乎不存在。 我的控制台说: Uncaught ReferenceError: jQuery17105683612572029233_1323808231542 is not defined 这是我的ajax请求: $.ajax({ url: ‘http://example.com’, data: { imgUrl: this.href, returnString:true }, dataType: “jsonp”, success: function(msg){ newNode.src = msg.data; }, error: function(msg){ console.log(msg.data); } })

将getJSON更改为JSONP

我有这个代码: $(document).ready(function() { $.getJSON(‘http://example.com/api/get_cats’, function(fbResults) { document.write(fbResults.cats[0].title); }); }); 我该如何更改此代码: $(document).ready(function() { $.getJSON(‘http://example.com/api/get_cats’, function(fbResults) { document.write(fbResults.cats[0].title); }); }); 它作为JSONP工作……这完全不同吗?

Jquery成功函数不使用JSONP触发

正在做一些使用jQuery调用我在不同域上的服务。 成功调用服务(我的调试点被触发),并返回正确的响应(我嗅探流量)。 我的问题主要是成功和失败的回调不会被解雇。 我已经在SO上阅读了一些其他post ,表明在使用JSONP时未触发错误事件。 是成功事件的情况(也许是因为我假设我提供自己的回调函数),或者有没有办法解雇我的成功回调。 提前致谢。 $.ajax({ type: “GET”, url: urlOnDiffDomain, async: false, cache: false, dataType: ‘jsonp’, data: {}, success: function(data, textStatus) { alert(‘success…’); }, error: function(xhr, ajaxOptions, thrownError) { alert(‘failed….’); } });

如何从ASP.NET Web服务生成JSONP以进行跨域调用?

我写了一个返回JSON的web服务,我试图用这样的jQuery调用它: $.ajax({ contentType: “application/json; charset=utf-8”, url: “http://examplewebsite.com/service.asmx/GetData”, data: { projectID: 1 }, dataType: “jsonp”, success: function () {alert(“success”);} }); 但是,尽管使用Fiddler查看HTTP流量时web服务调用成功,但代码从不调用成功函数。 我认为这是因为我的Web服务正在返回原始JSON而不是JSONP。 如何生成JSONP作为标准.NET Web服务方法的响应,如下所示: [WebMethod(), ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public Project GetData(int projectID) { Project p = new Project(); p.Name = “foobar”; return p; } 谢谢。

使用JQuery ajax的JSONP的回调函数

我不太明白如何使用JQuery的ajax函数的回调。 我在JavaScript中有以下代码: try { $.ajax({ url: ‘http://url.of.my.server/submit?callback=?’, cache: false, type: ‘POST’, data: $(“#survey”).serialize(), dataType: “jsonp”, timeout: 200, crossDomain: true, jsonp: ‘jsonp_callback’, success: function (data, status) { mySurvey.closePopup(); }, error: function (xOptions, textStatus) { mySurvey.closePopup(); } }); } catch (err) { mySurvey.closePopup(); } 在服务器端(AppEngine / Python),我得到回调参数的值并回复 self.response.headers[‘Content-Type’] = ‘application/json; charset=utf-8’ self.response.out.write(callback + ‘({“msg”: “ok”});’) 但后来我在浏览器上遇到”Error: jQuery152042227689944248825_1317400799214 […]

Wikipedia API是否支持CORS或仅支持JSONP?

这个问题与一年前提出的另一个问题有关。 作者询问如何使用JavaScript和Wikipedia API发出cros-origin请求,其中一条评论是: en.wikipedia.org似乎不允许使用CORS 并建议他改用JSONP。 我知道我可以使用JSONP,但如果我可以使用它,我更喜欢CORS。 我试过jsfiddle var url = “https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json”; $.ajax({ url: url, data: ‘query’, dataType: ‘json’, type: ‘POST’, headers: { ‘Api-User-Agent’: ‘Example/1.0’ }, origin: ‘https://jsfiddle.net/’, success: function (data) { console.log(data); //do something with data }}); 并得到以下错误: XMLHttpRequest无法加载https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json 。 对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 因此,不允许来源“ https://fiddle.jshell.net ”访问。 请求标题: authority:en.wikipedia.org method:OPTIONS path:/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json scheme:https accept:/ accept-encoding:gzip, deflate, sdch accept-language:en-US,en;q=0.8,fr-CA;q=0.6,fr;q=0.4,fr-FR;q=0.2,ru;q=0.2,uk;q=0.2 access-control-request-headers:accept, […]