最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Asynchronous cross-domain POST request via JavaScript? - Stack Overflow

programmeradmin5浏览0评论

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. I tried messing around with this using an iframe with a form inside, but no success.

Any ideas?

EDIT

unfortunately I have no control over the response data, it varies from XML, json to simple text.

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. I tried messing around with this using an iframe with a form inside, but no success.

Any ideas?

EDIT

unfortunately I have no control over the response data, it varies from XML, json to simple text.

Share Improve this question edited Jan 21, 2009 at 21:14 Luca Matteis asked Jan 21, 2009 at 21:08 Luca MatteisLuca Matteis 29.3k22 gold badges116 silver badges171 bronze badges 2
  • Why did your iframe endeavours fail? I have a hunch that it should work. Can you give the details of the approach you took? – Ateş Göral Commented Jan 21, 2009 at 21:23
  • If you have no control over the response data then wouldn't it be a security risk, because the page loaded on the iframe can access your page's DOM using window.parent. – andho Commented Oct 25, 2010 at 8:26
Add a ment  | 

5 Answers 5

Reset to default 12

You can capture the onload event of an iframe. Target your form to the iframe and listen for the onload. You will not be able to access the contents of the iframe though, just the event.

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. 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. The browser will load that "script" which then you can access from other javascript.

YUI3's IO object offers cross-domain requests, however it does so using a small Flash control it embeds on the page.

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.

You can't do anything cross-domain using javascript. You'd have to use a backend language like PHP or asp or something.

发布评论

评论列表(0)

  1. 暂无评论