admin管理员组文章数量:1336192
I'm having some trouble managing the answers to a simple XMLHttpRequest made on JS to my own server. I have some troubling answers, here's my code:
JavaScript:
function callPHP () {
var xml = new XMLHttpRequest();
xml.open("POST", "http://localhost/ajaxTest", false);
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send("format=json");
var resp = xml.responseText;
console.log(resp);
}
And PHP:
<?php
if (isset($_POST["format"])){
if($_POST["format"] == "json"){
echo '
{
"name": "Name",
"lastName": "LastName",
"dob" : "dd/mm/yyyy",
}
';
}
}else{
print_r($_POST);
echo "Error";
http_response_code(400);
}
?>
And whenever I execute my JS I get the error part of the PHP code, even having sent the "format=json" data on request. But if I change asynchronous to true, I get a GET error (in chrome's console) of 400 Bad Request, but no echo of the PHP is executed.
I know I have to check the xmlhttprequest status and response code to do asynchronous, I was testing it directly on the console.
I don't know what I'm doing wrong here, the POST data should be sent, and independently of asynchronous or not, right?
Thank you all!
I'm having some trouble managing the answers to a simple XMLHttpRequest made on JS to my own server. I have some troubling answers, here's my code:
JavaScript:
function callPHP () {
var xml = new XMLHttpRequest();
xml.open("POST", "http://localhost/ajaxTest", false);
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send("format=json");
var resp = xml.responseText;
console.log(resp);
}
And PHP:
<?php
if (isset($_POST["format"])){
if($_POST["format"] == "json"){
echo '
{
"name": "Name",
"lastName": "LastName",
"dob" : "dd/mm/yyyy",
}
';
}
}else{
print_r($_POST);
echo "Error";
http_response_code(400);
}
?>
And whenever I execute my JS I get the error part of the PHP code, even having sent the "format=json" data on request. But if I change asynchronous to true, I get a GET error (in chrome's console) of 400 Bad Request, but no echo of the PHP is executed.
I know I have to check the xmlhttprequest status and response code to do asynchronous, I was testing it directly on the console.
I don't know what I'm doing wrong here, the POST data should be sent, and independently of asynchronous or not, right?
Thank you all!
Share Improve this question asked Feb 19, 2015 at 23:05 Jo ColinaJo Colina 1,9248 gold badges29 silver badges49 bronze badges 5- 1 @developerwjk He's passing false as the third param to the open function, which means that it is synchronous. – Atli Commented Feb 19, 2015 at 23:14
- I see what you say, however my concern is more on the server side, why is it that I get a GET error when i set asynchronous to true? and why isn't the echo "Error" executed... I did try to execute a separate JS on the console directly, but I got the same answers – Jo Colina Commented Feb 19, 2015 at 23:15
-
1
Do you have some rewrite rule to make
http://localhost/ajaxTest
work? If not. maybe you're just missing the.php
extension there. – developerwjk Commented Feb 19, 2015 at 23:22 -
@developerwjk yes, you were right, I just had to add
index.html
to my URL, thanks! – Jo Colina Commented Feb 20, 2015 at 13:32 -
@developerwjk Do you happen to know how to add a rewrite rule to avoid inserting the
index.php
to the url? – Jo Colina Commented Feb 22, 2015 at 1:11
1 Answer
Reset to default 4looks like the structure of your javascript function is incorrect. You should setup a listener before you send the request. In your code perhaps something like:-
var xml = new XMLHttpRequest();
xml.onreadystatechange = function() {
if( xml.readyState==4 && xml.status==200 ){
console.log( xml.responseText );
}
};
xml.open("POST", "http://localhost/ajaxTest", false);
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send("format=json");
本文标签: javascriptXMLHttpRequest POST to PHPStack Overflow
版权声明:本文标题:javascript - XMLHttpRequest POST to PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742375110a2463004.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论