【发布时间】:2020-10-19 10:29:22
【问题描述】:
我想将我的数组保存在从 Javascript XMLHTTP 请求发送的数据库中。
Javascript
function xml2() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(JSON.parse(this.responseText));
}
};
xhttp.open("POST", "http://localhost/sekai_adminlte3_rnd/api/rnd/postdata", true);
var singlevar = {"country_name":"Indonesia","country_code":"ID"}
xhttp.send(singlevar);
}
laravel 中的控制器
public function postdata(Request $request)
{
$country = Country::create([
'country_code' => $request->get('country_code'),
'country_name' => $request->get('country_name'),
]);
return $country;
}
使用此代码我无法保存到数据库。
【问题讨论】:
-
请告诉我们您收到的错误。
-
您只是缺少添加:
$country->save(),它会起作用 -
我没有收到任何错误,我只是无法将数组保存到数据库:D
标签: javascript php laravel api xmlhttprequest