我已经使用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();
}
我的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);
}
现在我正在尝试将相同的流程应用到另一个Laravel 5.2项目但不知道该怎么做,特别是url (hello-world.php?edit = yes)部分来自哪里我会根据请求将数据发送到我的控制器。
到目前为止,我已经完成了这个:
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;
}
但只能输出直到警告(edit_pcname); 部分。
我的路线:
Route::post('updatelabdetails', 'LoginController@updateLabDetails');
我的控制器:
public function updateLabDetails(Request $request){
$post = $request->all();
var_dump($post);
die();
}
在使用MethodNotAllowedHttpException错误提交到 / showlabdetails?之类的网址后。
提前致谢。
答案 0 :(得分:0)
请更新您的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);
}
答案 1 :(得分:0)
您的laravel roure不正确您已注册POST
路线并访问GET
。
将Route::post('updatelabdetails', 'LoginController@updateLabDetails');
更改为Route::get('updatelabdetails', 'LoginController@updateLabDetails');
答案 2 :(得分:0)
如laravel文档中所述,您需要在请求中添加令牌。请参阅此链接https://laravel.com/docs/5.4/csrf