我发送给UPS的以下脚本,UPS页面计算运费。
<script type="text/javascript">
$('#shipping').submit(function(e){
var url = [
"http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
"10_action=3",
"13_product="+ $('* [name="selService"]').val(),
"14_origCountry="+ $('* [name="txtFromCountry"]').val(),
"15_origPostal="+ $('* [name="txtFromZip"]').val(),
"origCity="+ $('* [name="txtFromCity"]').val(),
"19_destPostal="+ $('* [name="txtToZip"]').val(),
"20_destCity="+ $('* [name="txtToCity"]').val(),
"22_destCountry="+ $('* [name="txtToCountry"]').val(),
"23_weight="+ $('* [name="txtPackWeight"]').val(),
"47_rateChart="+ $('* [name="selRate"]').val(),
"48_container="+ $('* [name="selPackaging"]').val(),
"49_residential="+ $('* [name="selResidential"]').val(),
"25_length="+ $('* [name="txtPackLength"]').val(),
"26_width="+ $('* [name="txtPackWidth"]').val(),
"27_height="+ $('* [name="txtPackHeight"]').val()
].join('&');
window.open(url); //you want to split output by '%' for it to make sense
e.preventDefault();
});
</script>
如何让它将生成的运费值返回给我的脚本。在PHP中我会使用类似的东西:
$fp = fopen($url, "r");
while(!feof($fp)){
$result = fgets($fp, 500);
$result = explode("%", $result);
$errcode = substr($result[0], -1);
switch($errcode){
case 3:
$returnval = $result[8];
break;
}
}
但我正在尝试这样做100%javascript / Jquery。 请帮忙。
答案 0 :(得分:1)
而不是做window.open(url);
你只是使用一个ajax函数,看起来你想要查看正在生成的url的get请求...
https://api.jquery.com/jQuery.get/
$.ajax({
url: url,
success: function(data) {
// data is the returned data
}
});
答案 1 :(得分:0)
如果您遇到安全问题,则无法向其他域“ups.com”发出请求,如果您出现以下错误
XMLHttpRequest cannot load http://www.ups.com/usin...
No 'Access-Control-Allow-Origin'
header is present on the requested resource.
Origin 'null' is therefore not allowed access.
研究“跨站点脚本”一词。