简体   繁体   English

Google Chrome扩展程序中的JSONP通信

[英]JSONP communication in a Google Chrome extension

I'm writing a Google Chrome extension. 我正在编写Google Chrome扩展程序。 I want to use jsonp cross-domain communication with jQuery. 我想在jQuery中使用jsonp跨域通信。 Here is the ajax code: 这是ajax代码:

$.ajax({
    type : 'POST',
    url : $(this).attr('action'),
    data : $(this).serialize(),
    contentType: 'application/json; charset=utf-8',
    dataType : 'jsonp',
    success : function() {
        alert('A');
    }
});

This calls this URL: 这称为以下URL:

http://sgsync.dev.kreatura.hu/api/signup/?callback=jQuery1710883696963544935_1327347078860&nick=&pass=&_=1327347087371 http://sgsync.dev.kreatura.hu/api/signup/?callback=jQuery1710883696963544935_1327347078860&nick=&pass=&_=1327347087371

The server answers 200 OK with this data: 服务器使用以下数据回答200 OK:

jQuery1710883696963544935_1327347078860({"messages":["Minden mez\u0151 kit\u00f6lt\u00e9se k\u00f6telez\u0151!"],"errorCount":1})

After that, i got this error message: 之后,我收到此错误消息:

Can't find variable: jQuery1710883696963544935_1327347078860

I tried everything and i can't understand the problem. 我尝试了一切,但我不明白问题所在。 Please help me! 请帮我!

Note that i programed the server-side code, so there could be a problem with that too. 请注意,我对服务器端代码进行了编程,因此也可能存在问题。

Thanks in advance! 提前致谢!

Part of the reason this is so confusing is because jQuery API confuses the issue of Ajax calls vs JSONP calls. 之所以如此令人困惑,部分原因是因为jQuery API混淆了Ajax调用与JSONP调用的问题。 When using $.ajax with dataType: 'jsonp' this does not do an Ajax call (no XHR communication is used) it instead uses dynamic script injection with a callback. 当将$.ajaxdataType: 'jsonp'这不会执行Ajax调用(不使用XHR通信),而是使用带有回调的动态脚本注入。 This means that the type: 'POST' will have no meaning (since dynamic script injection only works as a GET would work) and that all of the data will be encoded into the URL of the request as opposed to being send over as a post body. 这意味着type: 'POST'将没有任何意义(因为动态脚本注入仅可以像GET那样工作),并且所有data都将被编码到请求的URL中,而不是作为帖子发送身体。 If this is truly intended to "POST" data then JSONP should not be used (since sensitive data will be sent in clear text). 如果这确实是为了“ POST”数据,则不应使用JSONP(因为敏感数据将以明文形式发送)。

As mentioned in one of the comments, this issue was addressed in this answer with regards to JSONP requests from Chrome content scripts and using XHR from a content script. 正如评论中提到的那样,此问题解决此问题,涉及来自Chrome内容脚本的JSONP请求以及使用内容脚本的XHR。

JSONP request in chrome extension, callback function doesn't exist? chrome扩展中的JSONP请求,回调函数不存在?

With regards to Chrome Extensions, they do force you into a sandbox when using the "conten scripts" in a chrome extension. 对于Chrome扩展程序,当在chrome扩展程序中使用“内容脚本”时,它们确实您逼入沙箱。 You can remove the dataType: 'jsonp' form the request in the Chrome Extension content script and this call should work. 您可以在Chrome扩展程序内容脚本中从请求中删除dataType: 'jsonp' ,此调用应该可以正常工作。 If that does not work, you might trying making the call directly using the XHRHttpRequest: 如果那不起作用,您可以尝试使用XHRHttpRequest直接进行调用:

var xhr = new XMLHttpRequest();
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.open("POST", $(this).attr('action'), true);
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
       alert("A");
  }
}
xhr.send($(this).serialize());

With regards to other browsers, I am not sure how each of their specific plugin enviroments handle making cross domain XHR calls (or if they even allow it in the first place). 关于其他浏览器,我不确定它们的每个特定插件环境如何处理跨域XHR调用(或者是否一开始就允许)。 This is something that is NOT allowed from normal browsers (unless using something like easyXDM ). 这是普通浏览器所不允许的(除非使用easyXDM之类的东西 )。

Have a look at this question and my answer as I think it might give you a solution... 看看这个问题和我的回答,因为我认为这可能会为您提供解决方案...
Auto-load bookmarklet when in webpage as a Google Chrome extension 在网页中作为Google Chrome扩展程序自动加载小书签

Basic concepts of JSON-P: JSON-P的基本概念:

Insert a script tag which loads an external javascript file. 插入一个脚本标签,以加载外部javascript文件。
That file does nothing else than execute a pre-defined function, with the data from the server. 该文件除了使用服务器中的数据执行预定义功能外,什么也不做。

How to make it work: 如何运作:

First create a function, bound to the global object (window) : 首先创建一个绑定到全局对象(窗口)的函数:

window.processMyData = function processMyData(data) {
  console.log(data);
}

Then insert a script tag to the page:script = document.createElement("script"); 然后将脚本标签插入页面:script = document.createElement(“ script”);

$('<script></script>')
  .prop({'type': 'text/javascript', 'src': 'http://your.url?with=possible&data=in_it'})
  .appendTo('body');

You see? 你看? No need for the $.ajax wrapper, JSON-P works differently. 不需要$ .ajax包装器,JSON-P的工作方式有所不同。

Good luck! 祝好运!

Edit: as a response to Duskwuff I would like to add that I don't mean to say $.ajax is bad, or not useful. 编辑:作为对Duskwuff的回应,我想补充一点,我并不是说$.ajax不好,或者没有用。 I am not here to give you a jQuery code snippet, I am trying to let you understand your problem, with the help of a bit more basic javascript / html. 我不是在这里为您提供jQuery代码段,而是在更基本的javascript / html的帮助下,让您理解您的问题。 JSON-P is not just JSON with a P added, it's completely different from a normal request. JSON-P不仅是添加了P的JSON,而且与普通请求完全不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM