課程
/前端開發(fā)
/WebApp
/jQM Web App –列車時(shí)刻表
源碼下下來(lái)仍然無(wú)法跨域,老師有解決方法嗎
2015-10-26
源自:jQM Web App –列車時(shí)刻表 6-1
正在回答
從外界獲取的代理很容易失效,為解決跨域問(wèn)題,可以自行創(chuàng)建一個(gè)代理,此處使用php創(chuàng)建一個(gè)代理,保存在php_proxy_simple.php中,在客戶端調(diào)用web服務(wù)時(shí),向代理服務(wù)器提交一個(gè)URL地址,代理服務(wù)器使用CURL方法向?qū)嶋H的web服務(wù)發(fā)送請(qǐng)求,并將返回的XML數(shù)據(jù)發(fā)送給客戶端??蛻舳藢?duì)收到的XML數(shù)據(jù)再行解析。
index.html文件中的script代碼如下所示:主要修改了代理服務(wù)器地址,以及$.get方法中的參數(shù),index.html和php_proxy_simple.php文件都在train文件夾中,train文件夾在apache服務(wù)器的webapps
\projects文件夾中??筛鶕?jù)實(shí)際部署的位置自行調(diào)整urlProxy中的URL地址。
<script> ????var?urlProxy?=?"http://localhost:8080/projects/train/php_proxy_simple.php?ws_path=";?//跨域中轉(zhuǎn)??? ????var?url1?=?"http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeByStationName?UserID="; ????var?url2?=?"http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getStationAndTimeDataSetByLikeTrainCode?UserID="; ????var?url3?=?"http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx/getDetailInfoByTrainCode?UserID="; ????var?isbind?=?0; ????//獲取車次列表 ????var?getTrainList?=?function?()?{ ????????//數(shù)據(jù)校驗(yàn) ????????if?($("#search-no").val()?||?($("#search-begin").val()?&&?$("#search-end").val()))?{ ????????????var?searchButton?=?$(this); ????????????searchButton.button("option",?"disabled",?true); ????????????$.mobile.loading("show"); ????????????//var?_data?=?{}; ????????????var?_url?=?url1; ????????????if?(!$("#search-no").val())?{ ????????????????//_data.StartStation?=?$("#search-begin").val(); ????????????????//_data.ArriveStation?=?$("#search-end").val(); ????????????????_url?+="&StartStation="?+?$("#search-begin").val()+"&ArriveStation="?+?$("#search-end").val();?????????????? ????????????}?else?{ ????????????????//_data.TrainCode?=?$("#search-no").val(); ????????????????_url?=?url2; ????????????????_url?+=?"&TrainCode="?+?$("#search-no").val(); ????????????} ????????????$.get(urlProxy?+?encodeURIComponent(_url), ????????????????????function?(data)?{ ????????????????????????$("#list").html(""); ????????????????????????var?list?=?$("#list"); ????????????????????????var?timeTables?=?$(data).find("TimeTable"); ????????????????????????var?_arr?=?[]; ????????????????????????timeTables.each(function?(index,?obj)?{ ????????????????????????????var?i?=?index; ????????????????????????????if?(i?>?10)?return?false;?//只載入前10條 ????????????????????????????var?that?=?$(this); ????????????????????????????if?(that.find("FirstStation").text()?==?"數(shù)據(jù)沒(méi)有被發(fā)現(xiàn)")?{ ????????????????????????????????alert("數(shù)據(jù)沒(méi)有被發(fā)現(xiàn)!"); ????????????????????????????????return?false; ????????????????????????????} ????????????????????????????_arr.push('<li><a?href="#"?data-no="'?+?that.find("TrainCode").text()?+?'">'?+ ????????????????????????????????????'<h2>'?+?that.find("TrainCode").text()?+?'次</h2>'?+ ????????????????????????????????????'<p>'?+?that.find("FirstStation").text()?+?'?-?'?+?that.find("LastStation").text()?+?'</p>'?+ ????????????????????????????????????'<p>用時(shí):'?+?that.find("UseDate").text()?+?'</p>'?+ ????????????????????????????????????'<p?class="ui-li-aside">'?+?that.find("StartTime").text()?+?'?開</p>'?+ ????????????????????????????????????'</a></li>'); ????????????????????????}); ????????????????????????if?(_arr.length?>?0)?{ ????????????????????????????list.html(_arr.join("")); ????????????????????????????list.listview("refresh"); ????????????????????????} ????????????????????????$.mobile.loading("hide"); ????????????????????????searchButton.button("option",?"disabled",?false); ????????????????????}); ????????}?else?{ ????????????alert("請(qǐng)輸入發(fā)車站和終點(diǎn)站或輸入車次!"); ????????} ????}; ????var?isAjax=false ????//獲取詳情 ????var?getInfoByTrainCode?=?function?()?{ ????????$.mobile.loading("show"); ????????var?trainCode?=?$(this).attr("data-no"); ????????var?_url=url3; ????????if(isAjax)?return; ????????isAjax=true ????????_url?+=?"&TrainCode="?+?trainCode; ????????$.get(urlProxy?+?encodeURIComponent(_url), ????????????????function?(data)?{ ????????????????????isAjax=false ????????????????????$("#detail").find(".ui-content?h2").html(trainCode?+?"次"); ????????????????????var?tbody?=?$("#detail").find(".ui-content?tbody"); ????????????????????tbody.html(""); ????????????????????$(data).find("TrainDetailInfo").each(function?(index,?obj)?{ ????????????????????????var?tr?=?$("<tr></tr>"); ????????????????????????var?that?=?$(this); ????????????????????????tr.html('<td>'?+?that.find("TrainStation").text()?+?'</td>'?+ ????????????????????????????????'<td>'?+?that.find("ArriveTime").text()?+?'</td>'?+ ????????????????????????????????'<td>'?+?that.find("StartTime").text()?+?'</td>'); ????????????????????????tbody.append(tr); ????????????????????}); ????????????????????$.mobile.loading("hide"); ????????????????????$.mobile.changePage("#detail"); ????????????????}); ????}; ????//綁定事件 ????var?bindEvent?=?function?()?{ ????????$("#search-submit").on("click",?getTrainList); ????????$("#list").on("click",?"a",?getInfoByTrainCode); ????}; ????$(document).on("pageshow",?"#index",?function?()?{ ????????if?(isbind)?return ????????isbind?=?1; ????????bindEvent(); ????}); </script>
php_proxy_simple.php文件中的內(nèi)容如下所示:
<?php //?PHP?Proxy?example?for?Web?services.? //?Responds?to?both?HTTP?GET?and?POST?requests //?Get?the?REST?call?path?from?the?AJAX?application //?Is?it?a?POST?or?a?GET? $url?=?($_POST['ws_path'])???$_POST['ws_path']?:?$_GET['ws_path']; //?Open?the?Curl?session $session?=?curl_init(); curl_setopt($session,?CURLOPT_URL,?$url); //echo?$session; //?If?it's?a?POST,?put?the?POST?data?in?the?body if?($_POST['yws_path'])?{ $postvars?=?''; while?($element?=?current($_POST))?{ $postvars?.=?urlencode(key($_POST)).'='.urlencode($element).'&'; next($_POST); } curl_setopt?($session,?CURLOPT_POST,?true); curl_setopt?($session,?CURLOPT_POSTFIELDS,?$postvars); } //?Don't?return?HTTP?headers.?Do?return?the?contents?of?the?call curl_setopt($session,?CURLOPT_HEADER,?false); curl_setopt($session,?CURLOPT_RETURNTRANSFER,?true); //?Make?the?call $xml?=?curl_exec($session); echo?$xml; curl_close($session); ?>
舉報(bào)
主流移動(dòng)WEB應(yīng)用程序開發(fā)框架,可以開發(fā)簡(jiǎn)單的Web App應(yīng)用
14 回答無(wú)法跨域的原因?
2 回答跨域的問(wèn)題
5 回答跨域問(wèn)題失效
2 回答跨域問(wèn)題解決
2 回答跨域問(wèn)題不成功
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2015-12-25
從外界獲取的代理很容易失效,為解決跨域問(wèn)題,可以自行創(chuàng)建一個(gè)代理,此處使用php創(chuàng)建一個(gè)代理,保存在php_proxy_simple.php中,在客戶端調(diào)用web服務(wù)時(shí),向代理服務(wù)器提交一個(gè)URL地址,代理服務(wù)器使用CURL方法向?qū)嶋H的web服務(wù)發(fā)送請(qǐng)求,并將返回的XML數(shù)據(jù)發(fā)送給客戶端??蛻舳藢?duì)收到的XML數(shù)據(jù)再行解析。
index.html文件中的script代碼如下所示:主要修改了代理服務(wù)器地址,以及$.get方法中的參數(shù),index.html和php_proxy_simple.php文件都在train文件夾中,train文件夾在apache服務(wù)器的webapps
\projects文件夾中??筛鶕?jù)實(shí)際部署的位置自行調(diào)整urlProxy中的URL地址。
php_proxy_simple.php文件中的內(nèi)容如下所示: