将表单数据通过ajax调用创建到远程URL [英] Post form data created by an ajax call to a remote url

查看:186
本文介绍了将表单数据通过ajax调用创建到远程URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行一个两阶段的帖子。第一个是为我自己的服务创建一个AJAX帖子,创建表单数据,例如email = blah& dude = car等。

在AJAX调用回调I需要将这些数据重新发布到远程站点,在一个普通的帖子中。



我想的是这样的:

  var formData =某些表单数据; 
$ .ajax({
type:'POST',
url:'/ myservice',
data:formData,
成功:函数(数据,状态) {
xmlhttp = new XMLHttpRequest();
xmlhttp.open(POST,http://remotepage.com,true);
xmlhttp.send(data);
$,
error:function(XMLHttpRequest,textStatus,errorThrown){
//显示错误信息},
dataType:text,
});

但是,由于remotepage.com上的XSS阻止,httpRequest将失败。如何轻松地将处理过的表单数据重新发布到远程URL?

你意识到由于相同的来源政策限制发送AJAX请求至 http://remotepage.com xmlhttp.open(POST,http://remotepage.com,true); )不起作用,除非您的网站托管在 http://remotepage.com



所以为了达到这个目的,你需要在你的域名上设置一个服务器端脚本它将充当您的域和远程域之间的桥梁,然后您将向您的脚本发送一个AJAX请求。另外,因为你使用的是jQuery,所以在成功回调中使用它看起来更自然:

  var formData =some表格数据; 
$ .ajax({
type:'POST',
url:'/ myservice',
data:formData,
成功:函数(数据,状态) {
$ .post('/ some_bridge_script',formData);
},
错误:函数(XMLHttpRequest,textStatus,errorThrown){
//显示错误信息},
dataType:text,
});

如果远程域支持 JSONP ,你可以直接发送请求到它,但它仅限于GET请求。


I would like to perform a two stage post. The first is an AJAX post to my own service that creates form data such as "email=blah&dude=car" etc.

In the callback for the AJAX call I need to repost that data to a remote site, in a normal post.

I was thinking something like:

var formData = "some form data";
$.ajax({
    type: 'POST',
    url: '/myservice', 
    data: formData,
    success: function(data, status) {
             xmlhttp=new XMLHttpRequest(); 
                        xmlhttp.open("POST","http://remotepage.com",true); 
                        xmlhttp.send(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        //display error message     },
    dataType: "text",
});

However, the httpRequest will fail due to XSS prevention on remotepage.com. How can I easily repost the processed form data to the remote URL?

解决方案

You realize that due to same origin policy restrictions sending an AJAX request to http://remotepage.com (xmlhttp.open("POST","http://remotepage.com",true);) wouldn't work unless your site is hosted on http://remotepage.com.

So to achieve this you would need to setup a server side script on your domain which would act as bridge between your domain and the remote domain and you would then send an AJAX request to your script. Also because you are using jquery it would seem more natural to use it in the success callback as well:

var formData = "some form data";
$.ajax({
    type: 'POST',
    url: '/myservice', 
    data: formData,
    success: function(data, status) {
        $.post('/some_bridge_script', formData);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        //display error message     },
    dataType: "text",
});

If the remote domain supports JSONP you could directly send the request to it but it is only limited to GET requests.

这篇关于将表单数据通过ajax调用创建到远程URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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