简体   繁体   English

反向使用Ext.Ajax.request进行跨域POST请求

[英]Reverse engeering cross domain POST requests which use Ext.Ajax.request

I'm working with a script which seems to use Ext.Ajax.request (with ExtJS 3) to send cross-domain requests - some of them POST requests. 我正在使用一个似乎使用Ext.Ajax.request (使用ExtJS 3)发送跨域请求的脚本 - 其中一些是POST请求。 Considerations are being made to move away from ExtJS3 (perhaps move away from ExtJS in general) but a quick attempt at using XMLHttpRequest didn't work; 正在考虑摆脱ExtJS3(可能总体上远离ExtJS)但是使用XMLHttpRequest的快速尝试不起作用; how can I find out what technique is being used to send those cross domain requests? 如何找出用于发送这些跨域请求的技术?

I'm currently using ExtJS 3.3.1, I haven't made the switch to 4 yet but will most likely when a new project comes about. 我目前正在使用ExtJS 3.3.1,我还没有切换到4,但很可能在新项目出现时。 Without looking at the Ext source I can tell you they are using JSONP to accomplish this task, it is the only way to make a cross-domain AJAX call because JavaScript has to abide by the same-origin policy . 在不查看Ext源代码的情况下,我可以告诉您他们正在使用JSONP来完成此任务,这是进行跨域AJAX调用的唯一方法,因为JavaScript必须遵守同源策略

Are you trying to do a pure JS implementation of JSONP? 您是否尝试执行JSONP的纯JS实现? Or are you using a JS library already? 或者您已经在使用JS库了吗?

Edit 编辑

Per our comments... they are making POST requests. 根据我们的评论...他们正在发出POST请求。 That's not possible with JSONP. JSONP无法做到这一点。 So as far as I can tell, they are using iframe trickery similar. 所以据我所知,他们正在使用类似的iframe技巧。 It's the same trick used to "AJAX" upload files on older browsers. 这与旧版浏览器上“AJAX”上传文件的技巧相同。

This link explains it in more detail. 链接更详细地解释了它。

Also, the same method (iframe to, POST, upload a file) is used in Valum's file uploader . 此外,在Valum的文件上传器中使用相同的方法(iframe,POST,上传文件)。 It's much easier to follow then the ExtJS source. 然后更容易遵循ExtJS源。

The Ext JS 3.4 online documentation will provide you with the Ext.Ajax class inheritance model which can be used to trace the source code correlate to the Ext.Ajax.request method invocation. Ext JS 3.4在线文档将为您提供Ext.Ajax类继承模型,该模型可用于跟踪与Ext.Ajax.request方法调用相关的源代码。 However, rather than spending more time and resources in re-creating the wheel, I would suggest implementing the native Ext JS Ext.data.ScriptTagProxy class into your pre-existing stores via the proxy config option, to facilitate your cross-domain requests for remote stores. 但是,我建议不要花费更多时间和资源来重新创建轮子,而是建议通过proxy配置选项Ext.data.ScriptTagProxy生Ext JS Ext.data.ScriptTagProxy类实现到预先存在的商店中,以方便您的跨域请求远程商店。 Below is an abbreviated example of what I'm referring to. 下面是我所指的缩写示例。

Example

var myJsonStore = new Ext.data.JsonStore
({
    autoLoad : true,
    proxy : new Ext.data.ScriptTagProxy
    ({
        url : 'http://www.cross-domain.com/file.php'
    }),
    fields : ['myIdColumn','myCharColumn','myDateColumn']
});

ADDITION 加成

Because you intend on moving away from using Ext JS please checkout the ACD (AJAX Cross Domain) library. 因为您打算不再使用Ext JS,请检查ACD(AJAX Cross Domain)库。

JSONP is a bit of a hack, but usable. JSONP有点像黑客,但可用。

However, consider using CORS if you control the domains being crossed. 但是,如果您控制正在跨越的域,请考虑使用CORS。 CORS involves placing a header (Access-Control-Allow-Origin) in the responses from the web site: http://enable-cors.org/ CORS涉及在网站的响应中放置一个标题(Access-Control-Allow-Origin): http//enable-cors.org/

It's supported by IE 8+ (with caveat, natch), Firefox, and WebKit browsers. 它受IE 8+(带有警告,natch),Firefox和WebKit浏览器的支持。 The IE caveat is this: IE uses a different request object (XDomainRequest) for CORS requests. IE警告是这样的:IE对CORS请求使用不同的请求对象(XDomainRequest)。 If you have to support Opera you'll need to use JSONP or a polyfill (something like https://github.com/gimite/web-socket-js/ , which requires Flash). 如果你必须支持Opera,你需要使用JSONP或polyfill(类似https://github.com/gimite/web-socket-js/ ,需要Flash)。

If you don't control the domains in question, you can try asking them to support CORS. 如果您不控制相关域,可以尝试让它们支持CORS。

You can try to use jsonp Jquery example: 您可以尝试使用jsonp Jquery示例:

$.ajax({
  url: "test.php",
  dataType: "jsonp"
  success: function(data){
     console.log(data)
  }
});

Or if you have access to the requested content you can set the Access-Control-Allow-Origin header. 或者,如果您可以访问所请求的内容,则可以设置Access-Control-Allow-Origin标头。 PHP example: PHP示例:

header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);

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

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