Javascript Laravel 6从POST JSON获取信息
我试图从POST请求中获取JSON信息。我的职位要求是:Javascript Laravel 6从POST JSON获取信息,javascript,php,json,laravel,post,Javascript,Php,Json,Laravel,Post,我试图从POST请求中获取JSON信息。我的职位要求是: var money = document.getElementById("code").value; const Http = new XMLHttpRequest(); const url='/paypal/'; Http.open("POST", url); Http.setRequestHeader("Content-Type", "application/json"); Http.send(JSON.stringify({
var money = document.getElementById("code").value;
const Http = new XMLHttpRequest();
const url='/paypal/';
Http.open("POST", url);
Http.setRequestHeader("Content-Type", "application/json");
Http.send(JSON.stringify({
value: money
}));
控制器是:
public function paypal(Request $r) {
$postInput = file_get_contents('php://input');
$data = json_decode($postInput, true);
$response = array('value' => $data);
return Response::json($response);
}
但我得到的唯一结果是:
{"value":null}
知道我的问题出在哪里吗?使用POST请求时,不要忘记在头中设置csrf令牌 添加元标记
要从控制器检索JSON负载,请使用:
$data = json_decode($request->getContent(), true);
return response()->json($data);
使用POST请求时,请确保可变货币具有正确的值,不要忘记在标头中设置csrf令牌 添加元标记
要从控制器检索JSON负载,请使用:
$data = json_decode($request->getContent(), true);
return response()->json($data);
并确保变量money具有正确的值对于laravel,我们可以更轻松地获得POST JSON
use Illuminate\Http\Request;
public function test(Request $request) {
$inputData = $request->json()->all();
}
对于laravel,我们可以以更简单的方式获得POST JSON
use Illuminate\Http\Request;
public function test(Request $request) {
$inputData = $request->json()->all();
}
谢谢你们的帮助。我通过GET请求发布了所有内容,解决了我的问题。谢谢你们的帮助。我通过GET请求发布了所有内容,解决了我的问题。如果你在var money之后执行
console.log(money)
操作,你会得到null
还是你想要的值?我正在得到我想要的值。你能检查一下你的DevTools on Network选项卡,看看负载是否按照你想要的JSON格式发送吗?如果你执行console.log(money)
在var money之后,你得到的是null
还是你想要的值?我得到了我想要的值。你能检查一下你的DevTools on Network选项卡,看看有效负载是否按你想要的方式以JSON的形式发送吗?好的,我现在发送CSRF令牌,并尝试用你的方式检索JSON,但同样没有成功,我只得到了{}
好的,我现在正在发送CSRF令牌,并尝试用您的方式检索JSON,但同样没有成功,我只得到{}