[英]Post data using XMLHttpRequest to laravel 5.2 controller as request
I have done posting/updating successfully with javascript XMLHttpRequest() to php $_REQUEST like: 我已经成功地使用javascript XMLHttpRequest()将发布/更新到php $ _REQUEST,如下所示:
function ajax_edit(e_id){
var edit_form = document.getElementById('edit_form'+e_id);
var e_name = document.getElementById('name'+e_id).value,
e_email = document.getElementById('email'+e_id).value,
e_contact = document.getElementById('contact'+e_id).value,
e_status = document.getElementById('status'+e_id).value;
xmlhttp.open('GET', 'hello-world.php?edit=yes&id='+e_id+'&name='+e_name+'&email='+e_email+'&contact='+e_contact+'&status='+e_status, true);
xmlhttp.send();
$('#edit'+e_id).modal('hide');
return false;
edit_form.reset();
}
And my php work was like: 和我的PHP工作是这样的:
if(isset($_REQUEST['edit'])){
$name = mysqli_real_escape_string($conn, strip_tags($_REQUEST['name']));
$email = mysqli_real_escape_string($conn, strip_tags($_REQUEST['email']));
$contact = mysqli_real_escape_string($conn, strip_tags($_REQUEST['contact']));
$status = mysqli_real_escape_string($conn, strip_tags($_REQUEST['status']));
$edit_sql = "UPDATE users SET name = '$name', email = '$email', contact = '$contact', status = '$status' WHERE id = '$_REQUEST[id]'";
$run_edit = mysqli_query($conn, $edit_sql);
}
Now I am trying to apply the same process to another Laravel 5.2 project but don't know how to do, specially the url (hello-world.php?edit=yes) part from where I will send data to my controller as request. 现在,我正在尝试将相同的过程应用于另一个Laravel 5.2项目,但不知道该怎么做,特别是url (hello-world.php?edit = yes)部分,该部分是我根据请求将数据发送到控制器的。
So far I have done with this: 到目前为止,我已经做到了:
function submit_form(edit_id){
xmlhttp = new XMLHttpRequest();
var edit_form = document.getElementById('edit_form'+edit_id);
var url = "{{ URL::to('updatelabdetails'); }}";
var edit_labname = document.getElementById('labname'+edit_id).value,
edit_pcname = document.getElementById('pcname'+edit_id).value;
alert(edit_pcname);
var params = "labname='+edit_labname+'&pcname='+edit_pcname";
alert(params);
xmlhttp.open('GET', url+"?"+params, true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && http.status == 200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.send();
return false;
}
But only able to output till alert(edit_pcname); 但是只能输出到alert(edit_pcname); part.
部分。
My Route: 我的路线:
Route::post('updatelabdetails', 'LoginController@updateLabDetails');
My Controller: 我的控制器:
public function updateLabDetails(Request $request){
$post = $request->all();
var_dump($post);
die();
}
After submitting its going to some url like /showlabdetails? 将其提交到某些网址后,例如/ showlabdetails? with MethodNotAllowedHttpException error.
与MethodNotAllowedHttpException错误。
Thanks in advance. 提前致谢。
Please update your submit_form function like this 请像这样更新您的Submit_form函数
function submit_form(edit_id){
var edit_labname = document.getElementById('labname'+edit_id).value,
edit_pcname = document.getElementById('pcname'+edit_id).value;
var http = new XMLHttpRequest();
var url = "updatelabdetails";
var params = "labname='+edit_labname+'&pcname='+edit_pcname";
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
}
Your laravel roure isn't correct you've registered POST
route and accessing the GET
one. 您的laravel roure不正确,您已经注册了
POST
路由并访问了GET
路由。
Change Route::post('updatelabdetails', 'LoginController@updateLabDetails');
更改
Route::post('updatelabdetails', 'LoginController@updateLabDetails');
to Route::get('updatelabdetails', 'LoginController@updateLabDetails');
Route::get('updatelabdetails', 'LoginController@updateLabDetails');
you need to add token in your request as said in the laravel documentation.
您需要按照laravel文档中的说明在请求中添加令牌。 see this link https://laravel.com/docs/5.4/csrf
看到这个链接https://laravel.com/docs/5.4/csrf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.