AJAX提交表单到远程URL和XML效应初探提取网址 [英] AJAX submit form to remote URL and extract URL from XML reponse

查看:135
本文介绍了AJAX提交表单到远程URL和XML效应初探提取网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的JavaScript和Ajax。

我有一个职位远程URL,并返回一个包含一些数据和URL的XML响应的形式。我需要提取的URL和重定向到XML响应的URL。

下面是我的表格

 <形式的目标=_空白行动=的https://requesturl.co.uk/requestservice.asmx/Request_v4方法=POST>
<输入类型=隐藏值=''的名字='AffiliateID'/><输入类型=隐藏值=''的名字='AffiliatePW'/><输入类型=隐藏值= ''名称='ReqMode'/><输入类型=隐藏值=''的名字='ReqTotalTimeout'/><输入类型=隐藏值=''的名字='ReqLenderTimeout'/> <输入类型=隐藏值=''的名字='ReqLoanAmount'/><输入类型=隐藏值=''的名字='AppTitle'/><输入类型=隐藏值= ''名称='AppFirstName'/><输入类型=隐藏值=''的名字='AppLastName'/><输入类型=隐藏值=''的名字='AppEmail'/> <输入类型=隐藏值=''的名字='AppDOBDay'/><输入类型=隐藏值=''的名字='AppDOBMonth'/><输入类型=隐藏值= ''名称='AppDOBYear'/><输入类型=隐藏值=''的名字='AppHomePhone'/><输入类型=隐藏值=''的名字='AppWorkPhone'/> <输入类型=隐藏值=''的名字='AppMobilePhone'/><输入类型=隐藏值=''的名字='AppPost code'/><输入类型='隐藏'值=''的名字='AppHouseNumber'/><输入类型=隐藏值=''的名字='AppStreet'/><输入类型=隐藏值=''的名字='AppTown /><输入类型=隐藏值=''的名字='AppCounty'/><输入类型=隐藏值=''的名字='EmpIncomeType'/><输入类型='隐藏'值=''的名字='EmpEmployerName'/><输入类型=隐藏值=''的名字='EmpTimeAtEmployer'/><输入类型=隐藏值=''的名字='EmpNetMonthlyPay /><输入类型=隐藏值=''的名字='EmpPayFrequency'/><输入类型=隐藏值=''的名字='EmpDirectPayment'/><输入类型='隐藏'值=''的名字='EmpNextPayDay'/><输入类型=隐藏值=''的名字='EmpNextPayMonth'/><输入类型=隐藏值=''的名字='EmpNextPayYear /><输入类型=隐藏值=''的名字='EmpFollowingPayDay'/><输入类型=隐藏值=''的名字='EmpFollowingPayMonth'/><输入类型='隐藏'值=''的名字='EmpFollowingPayYear'/><输入类型=隐藏值=''的名字='EmpNINumber'/><输入类型=隐藏值=''名称='的BankAccount /><输入类型=隐藏值=''的名字='BankSort code'/><输入类型=隐藏值=''的名字='BankDebitCard'/><输入类型=隐藏值=''的名字='AppAddressYears'/><输入类型=隐藏值=''的名字='AppAddressMonths'/><输入类型=隐藏值='' NAME ='AppHomeStatus'/><输入类型=隐藏值=''的名字='ReqConsent'/><输入类型=隐藏值=''的名字='ReqIPAddress'/><输入类型=隐藏值=''的名字='ReqAgent'/><输入类型=隐藏值=''的名字='ReqMarketingOptIn'/><输入类型=隐藏值='' NAME ='ReqDomain'/>
<输入类型=提交值=调用级=按钮>
 

XML响应是

<$p$p><$c$c><RequestResult><Result>Accepted</Result><FailureReasons/><AcceptedLender>LendingStream</AcceptedLender><RedirectURL>http://redirecttothisURL.com/testing.htm</RedirectURL><PPDReference>6524845</PPDReference></RequestResult>

在一些研究它看起来像jQuery的话最好要做到这一点,但我越来越糊涂了,如果实际上它发布到远程URL将创建由于浏览器不会允许它的问题。

任何帮助或想法将不胜AP preciated。

感谢

解决方案

是,由于同源策略限制不能使用JavaScript来执行这一要求。你可以写一个服务器端脚本,该脚本将执行,以获取远程资源,然后使用一个AJAX请求到您的域名此服务器端脚本将把XML请求,并允许您解析。

显然,这取决于您使用的是可能有不同的方法来实现这个服务器端语言。

但是,如果你想使用jQuery是pretty的轻松,一旦你已经安装在你的域名此服务器端脚本。您只需使用 $。阿贾克斯() 方法:

  $。阿贾克斯({
    网址:'/ url_to_your_server_side_script_that_serves_as_a_bridge,
    成功:函数(XML){
        警报($('结果',XML)的.text());
    }
});
 

和这里的href="http://jsfiddle.net/3jt3e/" rel="nofollow">现场演示一个

I am new to JavaScript and Ajax.

I have a form which posts to remote url and returns a XML response containing some data and a URL. I need to extract the url and redirect to the URL in the XML response.

Here is my form

<form target="_blank" action='https://requesturl.co.uk/requestservice.asmx/Request_v4' method="POST"  >
<input type='hidden' value='' name='AffiliateID' /><input type='hidden' value='' name='AffiliatePW' /><input type='hidden' value='' name='ReqMode' /><input type='hidden' value='' name='ReqTotalTimeout' /><input type='hidden' value='' name='ReqLenderTimeout' /><input type='hidden' value='' name='ReqLoanAmount' /><input type='hidden' value='' name='AppTitle' /><input type='hidden' value='' name='AppFirstName' /><input type='hidden' value='' name='AppLastName' /><input type='hidden' value='' name='AppEmail' /><input type='hidden' value='' name='AppDOBDay' /><input type='hidden' value='' name='AppDOBMonth' /><input type='hidden' value='' name='AppDOBYear' /><input type='hidden' value='' name='AppHomePhone' /><input type='hidden' value='' name='AppWorkPhone' /><input type='hidden' value='' name='AppMobilePhone' /><input type='hidden' value='' name='AppPostCode' /><input type='hidden' value='' name='AppHouseNumber' /><input type='hidden' value='' name='AppStreet' /><input type='hidden' value='' name='AppTown' /><input type='hidden' value='' name='AppCounty' /><input type='hidden' value='' name='EmpIncomeType' /><input type='hidden' value='' name='EmpEmployerName' /><input type='hidden' value='' name='EmpTimeAtEmployer' /><input type='hidden' value='' name='EmpNetMonthlyPay' /><input type='hidden' value='' name='EmpPayFrequency' /><input type='hidden' value='' name='EmpDirectPayment' /><input type='hidden' value='' name='EmpNextPayDay' /><input type='hidden' value='' name='EmpNextPayMonth' /><input type='hidden' value='' name='EmpNextPayYear' /><input type='hidden' value='' name='EmpFollowingPayDay' /><input type='hidden' value='' name='EmpFollowingPayMonth' /><input type='hidden' value='' name='EmpFollowingPayYear' /><input type='hidden' value='' name='EmpNINumber' /><input type='hidden' value='' name='BankAccount' /><input type='hidden' value='' name='BankSortcode' /><input type='hidden' value='' name='BankDebitCard' /><input type='hidden' value='' name='AppAddressYears' /><input type='hidden' value='' name='AppAddressMonths' /><input type='hidden' value='' name='AppHomeStatus' /><input type='hidden' value='' name='ReqConsent' /><input type='hidden' value='' name='ReqIPAddress' /><input type='hidden' value='' name='ReqAgent' /><input type='hidden' value='' name='ReqMarketingOptIn' /><input type='hidden' value='' name='ReqDomain' />
<input type="submit" value="Invoke" class="button">

The XML response is

<RequestResult><Result>Accepted</Result><FailureReasons/><AcceptedLender>LendingStream</AcceptedLender><RedirectURL>http://redirecttothisURL.com/testing.htm</RedirectURL><PPDReference>6524845</PPDReference></RequestResult>

after some research it looks like Jquery would be best to do this but I'm getting confused if the fact its posting to remote URL will create problems due to browsers not allowing it.

Any help or ideas would be greatly appreciated.

Thanks

解决方案

Yes, due to the same origin policy restriction you cannot use javascript to perform this request. You could write a server side script that will perform the request to fetch the remote resource and then use an AJAX request to this server side script on your domain that will bring the XML and allow you to parse it.

Obviously depending on the server side language you are using there might be different ways to implement this.

But if you want to use jQuery it is pretty easy once you have setup this server side script on your domain. You simply use the $.ajax() method:

$.ajax({
    url: '/url_to_your_server_side_script_that_serves_as_a_bridge',
    success: function(xml) {
        alert($('Result', xml).text());
    }
});

And here's a live demo.

这篇关于AJAX提交表单到远程URL和XML效应初探提取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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