admin管理员组文章数量:1332872
Hello guys I just started learning php. So I was trying to make a post request from my javascript file to my php file by Fetch API
when I make the request in my console I'm getting this error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
I don't understand why this error is happening. Please help me fix this problem.
JavaScript
// payload object
const data = {
first_name: "first name",
last_name: "last name"
}
// make the request
fetch("ajax-insert.php", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then((response) => response.json())
.then((data) => console.log(data))
PHP
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
echo $first_name;
echo "<br />";
echo $last_name;
?>
Hello guys I just started learning php. So I was trying to make a post request from my javascript file to my php file by Fetch API
when I make the request in my console I'm getting this error Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
I don't understand why this error is happening. Please help me fix this problem.
JavaScript
// payload object
const data = {
first_name: "first name",
last_name: "last name"
}
// make the request
fetch("ajax-insert.php", {
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then((response) => response.json())
.then((data) => console.log(data))
PHP
<?php
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
echo $first_name;
echo "<br />";
echo $last_name;
?>
Share
Improve this question
edited Mar 11, 2022 at 19:53
Udochukwu Enwerem
2,8432 gold badges26 silver badges35 bronze badges
asked Mar 11, 2022 at 17:44
Fuhad HasanFuhad Hasan
532 gold badges2 silver badges9 bronze badges
1
- from your question, you are sending json data via post to a php script and expecting a json response. If you want the required json response, you are not expected to have more than one echo statement. see my answer. – Udochukwu Enwerem Commented Mar 11, 2022 at 20:27
2 Answers
Reset to default 3You are getting this error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0, because an HTML error response is being returned to the browser instead of json data.
In PHP, use file_get_contents("php://input")
to get the string json and json_decode
to get a php object as shown below. Note that what your javascript fetch call is expecting as returned data is only the json string
PHP
<?php
$json = file_get_contents("php://input"); // json string
echo $json;
// The following lines are only useful inside the php script
// $object = json_decode($json); // php object
// $first_name = $object->first_name;
// $last_name = $object->last_name;
// echo $first_name;
// echo "<br />";
// echo $last_name;
?>
First your js file is missing one curly brace
method: "POST",
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json; charset=UTF-8"
}
})
.then((response) => response.json())
.then((data) => console.log(data))
Also, in your php, you need send back a json something like
<?php
$content = trim(file_get_contents("php://input"));
//this content should be a json already
//{"first_name":"first name","last_name":"last name"}
//if want to access values
$_arr = json_decode($content, true);
$first_name = $_arr["first_name"];
$last_name = $_arr["last_name"];
//do what ever you want to do with first_name and last_name
//after you are done, be sure to send back a json
echo json_encode(['success'=>true]);
exit()
本文标签: javascriptHow to send request to php file via Fetch API post methodStack Overflow
版权声明:本文标题:javascript - How to send request to php file via Fetch API post method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742314498a2451541.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论