[英]Redirecting using php doesn't work
我有一個頁面,用戶可以在其中填寫他的電子郵件地址。 當用戶單擊“發送”時,將調用一個.php腳本。 在此腳本中,將對外部Web服務進行POST,並收到包含URL的答案。 然后,.php導航到該URL。
看起來像這樣:
<!DOCTYPE html>
<html>
<head>
<title>Email</title>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<link rel="stylesheet" href="themes/customtheme.css" />
<link rel="stylesheet" href="style.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
</head>
<body>
<div data-role="page">
<div data-role="header">
<a class="ui-btn-left" href="index.html" data-icon="back">Back</a>
<h1><span></span></h1>
<a class="ui-btn-right" href="#" data-icon="info">i & €</a>
</div>
<div data-role="content" data-position="relative">
<div class="inform">
<form method="POST" class="thedataform" position:relative action="send.php" >
<span>Email adress:</span>
<input type="text" name="email" id="email"></input>
<div>
<input type="submit" style="width:100%" name="smsbutton" value="Send" >
</div>
</form
</div>
</div>
<div data-role="footer" data-position="fixed">
</div>
</div>
</body>
</html>
send.php看起來像這樣:
<?php
$xml = file_get_contents('xml.xml');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://other.webservice/yyy/");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$sXML = curl_exec($ch);
curl_close($ch);
$oXML = simplexml_load_string($sXML);
$url = (string) $oXML->RETURNEDURL;
if (isset($_POST["email"]))
{
$email = $_POST["email"];
}
else
{
$email = null;
}
session_start();
$_SESSION['email'] = $email;
setcookie("email", $email);
header('Location: '.$url);
exit;
?>
現在,當我直接導航到send.php時,一切正常。 我被重定向到另一個網站(RETURNEDURL)。
但是,當我想使用Submit按鈕運行.php時,出現了錯誤。 由於我正在使用JQuery和JQuery Mobile,因此收到消息“錯誤加載頁面”。 我的FireBug中彈出以下消息:
[12:53:31.097] POST http://myserver.com/send.php [HTTP / 1.1 302臨時移動1456ms]
當我注釋掉以下兩行之一時:
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
重定向有效,但是我的頁面很難弄亂。 因為我正在從HTTP轉到HTTPS帖子,是否可能發生錯誤? 這里有什么問題?
我將網站更改為使用以下腳本:
<script type="text/javascript">
$(function() {
$("#emailForm").submit(function() {
console.log("Submit prevented.");
$.ajax({
type: "POST",
url: "send.php",
data: {
email: $("#email").val()
},
success: function(data) {
var obj = jQuery.parseJSON(data);
window.location = obj.url;
}
});
return false;
});
});
</script>
當我將send.php更改為以下內容時:
<?php
$xml = file_get_contents('xml.xml');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://the.website/xxx/");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$sXML = curl_exec($ch);
curl_close($ch);
$oXML = simplexml_load_string($sXML);
$url = (string) $oXML->RETURNEDURL;
if (isset($_POST["email"]))
{
$email = $_POST["email"];
} else {
$email = null;
}
session_start();
setcookie("email", $email);
$arr = array("url"=>$url);
echo(json_encode($arr));
?>
現在它可以正常工作了。
將data-ajax =“ false”屬性添加到您的表單,它應該可以在第一個版本中按預期工作。 有關詳細信息,請訪問http://jquerymobile.com/demos/1.2.1/docs/forms/forms-sample.html 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.