简体   繁体   English

通过JavaScript进行异步跨域POST请求?

[英]Asynchronous cross-domain POST request via JavaScript?

I could just create a form and use that to do a POST request to any site, thing is the FORM method isn't asynchronous, I need to know when the page has finished loading. 我可以创建一个表单并使用它来向任何站点发出POST请求,事情是FORM方法不是异步的,我需要知道页面何时完成加载。 I tried messing around with this using an iframe with a form inside, but no success. 我尝试使用带有表单的iframe来解决这个问题,但没有成功。

Any ideas? 有任何想法吗?

EDIT 编辑

unfortunately I have no control over the response data, it varies from XML, json to simple text. 遗憾的是,我无法控制响应数据,它从XML,json到简单文本都有所不同。

You can capture the onload event of an iframe . 您可以捕获iframeonload事件。 Target your form to the iframe and listen for the onload. form到iframe并监听onload。 You will not be able to access the contents of the iframe though, just the event. 您将无法访问iframe的内容,只是事件。

Try something like this: 尝试这样的事情:

<iframe id='RS' name='RS' src='about:blank' onload='loaded()'></iframe>

<form action='wherever.php' target='RS' method='POST'>...</form>

script block: 脚本块:

var loadComplete = 0
function loaded() {
    //avoid first onload
    if(loadComplete==0) {
        loadComplete=1
        return()
    }
    alert("form has loaded")
}

IF you want to make cross domain requests you should either made a JSON call or use a serverside proxy. 如果您想要发出跨域请求,您应该进行JSON调用或使用服务器端代理。 A serverside proxy is easy to set up, not sure why people avoid it so much. 服务器端代理很容易设置,不确定为什么人们会这么做。 Set up rules in it so people can not use the proxy to request other things. 在其中设置规则,以便人们不能使用代理来请求其他事情。

If the data returned from the cross domain post is JSON, then you can dynamically add a script tag pointing to the URI that returns the data. 如果从跨域帖子返回的数据是JSON,则可以动态添加指向返回数据的URI的脚本标记。 The browser will load that "script" which then you can access from other javascript. 浏览器将加载该“脚本”,然后您可以从其他JavaScript访问。

YUI3 's IO object offers cross-domain requests, however it does so using a small Flash control it embeds on the page. YUI3IO对象提供跨域请求,但它使用嵌入在页面上的小型Flash控件。

While there is work going into secure cross-domain requests from JavaScript, at this time, you need to use a plugin like Flash or Silverlight as a bridge with which to make the request. 虽然有一些工作涉及来自JavaScript的安全跨域请求,但此时,您需要使用Flash或Silverlight等插件作为发出请求的桥梁。

You can't do anything cross-domain using javascript. 你不能使用javascript跨域进行任何操作。 You'd have to use a backend language like PHP or asp or something. 你必须使用像PHP或asp之类的后端语言。

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

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