如何发布到其他域,并在javascript中获取结果html [英] How to post to other domain and get result html in javascript

查看:73
本文介绍了如何发布到其他域,并在javascript中获取结果html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览器必须发送POST请求并从javascript接收结果html页面。
在服务器响应中没有Access-Control-Allow-Origin。这是不可能改变任何东西是服务器。



如果人类点击是提交按钮,响应返回propery。



  $ .ajax('https://example.com',{
数据:
{
postkey:'value'
}
方法:'POST',
async:false
})
。 (函数(data,textStatus,jqXHR){
alert(data);
})
.fail(函数(xhr,textStatus,errorThrown){
alert(JSON。 stringify(xhr));
});

在Chrome控制台中返回异常:

 否请求的资源上存在'Access-Control-Allow-Origin'标头。原因'http:// localhost:52216'因此不被允许访问。 

我也试过jsonp

  $ .ajax('https://example.com',{
data:
{
postkey:'value'
}
方法:'POST',
dataType:'jsonp',
async:false
})
.done(function(data,textStatus,jqXHR){
alert(data);
})
.fail(function(xhr,textStatus,errorThrown){
alert(JSON.stringify(xhr));
});

在这种情况下,请求类型更改为GET,并在chrome控制台中出现stange错误:

 未捕获的SyntaxError:意外的标记< 

Chrome开发者工具显示,在这两种情况下,example.com服务器都返回了正确的html:

 <!DOCTYPE HTML PUBLIC -  // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org /TR/html4/loose.dtd\"> 
< html lang =et>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
< meta http-equiv =content-languagecontent =et>
< meta http-equiv =X-UA-Compatiblecontent =IE = Edge,chrome = 1>
...

如何在客户端修复此问题?我需要模拟点击表单提交按钮并获取结果html页面。



使用Jquery,Bootstrap 3和jQuery UI。
在服务器Apache中,使用Mono,mod_mono,ASP .NET MVC 4.6。

在服务器中创建MVC控制器来调用这样的请求并返回结果是可能的。我正在寻找一种方法来从浏览器中做到这一点,而不需要额外的应用程序自己的服务器调用。

**更新**



From
如何通过JavaScript发送跨域POST请求? jcubic answer我发现样本:


还有一种方法(使用html5功能)。您可以使用在其他域上托管的代理iframe
,使用postMessage将消息发送到
表示iframe,然后该iframe可以执行POST请求(在同一个域中)和
postMessage,并返回到reposnse

parent.com上的父母

  var win = $('iframe')[0] .contentWindow; 

win.postMessage(JSON.stringify({url:URL,data:{}}),http://reciver.com);
$ b $ function get(event){
if(event.origin ===http://reciver.com){
// event.data是来自POST的响应



if(window.addEventListener){
addEventListener(message,get,false)
} else {
attachEvent(onmessage,get)
}

iframe on reciver.com

 函数监听器(事件){
if(event.origin ===http://sender.com){
var data = JSON.parse(event.data);
$ .post(data.url,data.data,function(reponse){
window.parent.postMessage(reponse,*);
});


//不知道我们是否可以在这里使用jQuery
if(window.addEventListener){
addEventListener(message,listener,false )
} else {
attachEvent(onmessage,listener)
}

这也在 http://www.ajax-cross-origin .com / how.html



这是否适用于目前的浏览器,这是否合理?
是否有一些泛型方法或插件实现了这一点?

解决方案

除非你拥有你发布的域名,您将无法发布到发起请求的域以外的其他域。


Browser must send POST request and receive result html page from javascript. There is no Access-Control-Allow-Origin in server response. It is not possible to change anything is server.

If human clicks is submit button, response is returned propery.

Trying same from javascript using:

        $.ajax('https://example.com', {
            data: 
                {
                    postkey: 'value'
                }
            method: 'POST',
            async: false
        })
            .done(function (data, textStatus, jqXHR) {
                alert(data);
            })
            .fail(function (xhr, textStatus, errorThrown) {
                alert(JSON.stringify(xhr));
            });

returns exception in chrome console:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:52216' is therefore not allowed access.

I also tried jsonp

        $.ajax('https://example.com', {
            data: 
                {
                    postkey: 'value'
                }
            method: 'POST',
            dataType: 'jsonp',
            async: false
        })
            .done(function (data, textStatus, jqXHR) {
                alert(data);
            })
            .fail(function (xhr, textStatus, errorThrown) {
                alert(JSON.stringify(xhr));
            });

In this case request type is changed to GET and stange error in chrome console occurs:

Uncaught SyntaxError: Unexpected token <

Chrome developer tools shows that in both cases example.com server returned proper html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="et">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="content-language" content="et">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
...

How to fix this in client side ? I need to emulate click in form submit button and get result html page.

Jquery, Bootstrap 3 and jquery UI are used. In server Apache, Mono, mod_mono, ASP .NET MVC 4.6 are used.

It is possbile to create MVC controller in server which invokes such request and returns result. I'm looking for a way to do it from browser, without extra application own server call.

** Update **

From How do I send a cross-domain POST request via JavaScript? jcubic answer I found sample:

There is one more way (using html5 feature). You can use proxy iframe hosted on that other domain, you send message using postMessage to that iframe, then that iframe can do POST request (on same domain) and postMessage back with reposnse to the parent window.

parent on sender.com

var win = $('iframe')[0].contentWindow;

win.postMessage(JSON.stringify({url: "URL", data: {}}),"http://reciver.com");

function get(event) {
    if (event.origin === "http://reciver.com") {
        // event.data is response from POST
    }
}

if (window.addEventListener){
    addEventListener("message", get, false)
} else {
    attachEvent("onmessage", get)
}

iframe on reciver.com

function listener(event) {
    if (event.origin === "http://sender.com") {
        var data = JSON.parse(event.data);
        $.post(data.url, data.data, function(reponse) {
            window.parent.postMessage(reponse, "*");
        });
    }
}
// don't know if we can use jQuery here
if (window.addEventListener){
    addEventListener("message", listener, false)
} else {
    attachEvent("onmessage", listener)
}

Also this is described in http://www.ajax-cross-origin.com/how.html

Will this work on current browsers and is this resonable ? Is there some generic method or plugin which implements this ?

解决方案

Unless you own the domain you are posting to, you won't be able to post to a different domain from the one that originated the request.

这篇关于如何发布到其他域,并在javascript中获取结果html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆