如何获取http响应码
我有一个将值发送到变量 $_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 有任何问题请纠正我
I have a form which sends the values to the variables $_POST['person_id'] , $_POST['accident_loc'], $_POST['message'] and $_POST['name'].
The php file inserts the values as a record in the database, i dont have any problem with that. But i want to return an error when the form doesn't send any values to the variables.
I mean when i submit a form without any values. so i made an if statement if(isset($_POST['person_id']) && isset($_POST['accident_loc']) && isset($_POST['message']) && isset($_POST['name'])){}
if this statement fails call the function "sendResponse()".
now i want this file to return the status code 400 to my javascript file so that i can write some error conditions. But when i tried to display the value of the status code request.status
it gave me 0 as its values
Below is my php file.. can you tell me how to send the status code ??
<?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();
?>
UPDATE:
here is my 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);
}
if you find any problem in my javascript pls correct me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你做得几乎是正确的,这是我在我这边是如何做到的
在第二个文件中:
所有这些都可以在 jQuery 网站上找到,只需根据你的需要进行调整,使其在你的系统中工作:< a href="http://api.jquery.com/jQuery.get/" rel="nofollow">http://api.jquery.com/jQuery.get/
祝你好运
You are doing it almost right, here is how i was able to do it on my side
And in the second file:
All of this is available on the jQuery website, just adapt it to your needs to make it work in your system: http://api.jquery.com/jQuery.get/
Good luck
而不是
发送响应();
只需发送正确的 HTTP 错误代码即可。
该线路
将为您做到这一点。有关所有有效状态代码的列表,请查看
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Instead of
SendResponse();
just send the proper HTTP error code.
The line
will do that for you. For a list of all valid status codes have a look at
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes