[英]how to get http response code
我有一個將值發送到變量$ _POST ['person_id'],$ _POST ['accident_loc'],$ _POST ['message']和$ _POST ['name']的表單。
php文件將值插入為數據庫中的記錄,我對此沒有任何問題。 但是,當表單不向變量發送任何值時,我想返回一個錯誤。
我的意思是當我提交沒有任何值的表格時。 所以我做了一個if語句if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name'])){}
如果此語句失敗,則調用函數“ sendResponse()”。
現在,我希望此文件將狀態代碼400返回到我的JavaScript文件,以便我可以編寫一些錯誤條件。 但是當我嘗試顯示狀態代碼request.status
的值時,它給了我0作為其值
以下是我的php文件..您能告訴我如何發送狀態代碼嗎?
<?php
//allowing access to the server which contains the following host-origin
if($_SERVER[HOST_ORIGIN]=="http:\\localhost")
header('Access-Control-Allow-Origin:http:\\http:localhost');
function sendResponse(){
header('HTTP/1.1 400 invalid request');
header('Content-type: text/html');
echo "invalid request";
}
if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name']))
{
//php mysql database info
require ("phpMysql_dbInfo.php");
//getting the arguments from the url
$person_id=$_POST['person_id'];
$accident_loc=$_POST['accident_loc'];
$message=$_POST['message'];
$name=$_POST['name'];
//connecting to the database
$connection= mysql_connect($local_host,$username,$password);
if(!$connection)
die(mysql_error()."</br>");
//selecting the db
$select_db= mysql_select_db($database_name);
if(!$select_db)
die(mysql_error()."</br>");
//inserting the values into the database
$query= sprintf("insert into messages_to_people(name,accident_location,message,person_id)
values('%s','%s','%s','%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($accident_loc),
mysql_real_escape_string($message),
mysql_real_escape_string($person_id));
$result=mysql_query($query);
if(!$result)
echo mysql_error().'</br>';
}
else
sendResponse();
?>
更新:這是我的javascript
function downloadXml (url,params,callback) {
var request= window.XMLHttpRequest ? new XMLHttpRequest(): new ActiveXObject('Microsoft.XMLHTTP');
request.onreadystatechange= function(){
if(request.readyState==4){
//gives me a zero when i try to display it through alert
alert(request.status);
callback(request, request.status);
}
};
request.open('POST',url,true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send(params);
}
如果您在我的JavaScript中發現任何問題,請糾正我
您的操作幾乎正確,這就是我能夠做到的一面
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
$(function(){
alert('getting page');
jqXHR = $.get(
'index.php',
function(data){
alert(data);
}
)
.success(function(){ alert('second success'); })
.error(function(){ alert('error'); })
.complete(function(){ alert('complete'); });
});
</script>
並在第二個文件中:
<?php header('HTTP/1.1 400 Bad Request'); ?>
Invalid request
所有這些都可以在jQuery網站上找到,只需對其進行調整即可使其在您的系統中正常工作: http : //api.jquery.com/jQuery.get/
祝好運
代替SendResponse(); 只需發送正確的HTTP錯誤代碼即可。
線
header('HTTP/1.1 400 Bad Request');
會為你做的。 有關所有有效狀態代碼的列表,請訪問http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.