關於 Laravel 中 Ajax 問題的小結

CN發表於2017-06-15

環境:phpstudy php:v5.6.27+apache 編輯器:phpstrom 大環境:win7
在使用ajax過程中遇到一些錯誤,現在總結一下。主要是為了提醒自己,順便幫助一下大家。有補充的歡迎來mail
1.0 首先是使用jQuery的ajax,報錯161,也就是傳統路由錯誤,
解決方法:x%,為錯誤概率
1.1去看route檔案,確認沒問題 10%
1.2去看控制器和方法,是否存在 1%
1.3去看前臺ajax的URL,我錯誤的時候是“/front/index/ajax”,我改成了“http://localhost/front/index/ajax”後,顯示正確
2.0 解決完161後會出現csrf錯誤,得驗證token值,我在blade中新增了filefile然後token值就驗證成功了
3.0 解決完token值,會報錯
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0

Warning: Cannot modify header information - headers already sent in Unknown on line 0
到了這一步,已經可以檢視訊息了,可以通過以下在後臺controller的方法ajax中使用,從瀏覽器檢視是什麼http問題
// 判斷傳遞引數的形式
var_dump( $request->method()); // methed
// 獲取URL
$url = $request->url();
// 將URL拆分
$path = parse_url($url);
// 從URL中獲取引數 Route::get('/front/index/canton/{country?}','Front
我犯下這個錯誤的原因是我傳值時把data寫成了date,所以post的URL沒有帶上值,這是粗心犯的錯,希望你們沒有。
沒帶上值是我通過 var_dump($request->url());檢視到的,正常網址是:http://localhost/front/index/ajax/parm
報錯網址是:http://localhost/from/index/ajax

這裡附加一下我參考過的地址,如果我的回答沒解決你們的問題,希望下面的網址有所幫助:
解決token驗證的參考:http://blog.csdn.net/woshihaiyong168/artic...
其他解決token驗證的參考:http://eyehere.net/2015/laravel-ajax-csrf/
原生ajax(xmlHttp)的用法的參考:http://blog.csdn.net/linzhiqiang0316/article/details/52326773和http://www.jb51.net/article/73936.htm



本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章