首页 > 解决方案 > 尽管我的路径正确,但在地图上显示某些内容时,我在 laravel 中收到错误 419

问题描述

我使用 google maps api 创建了一个应用程序,该应用程序允许用户在特定位置添加标记,但是当我单击地图添加位置时,我返回的所有数据都是正确的,但我得到错误 19 的响应代码。 .这是我保存数据的代码

function saveData(lat,lng) 
{

console.log("LAT:"+lat+" LNG:"+lng);

var description = document.getElementById('manual_description').value;
var formData = new FormData();
console.log("DESC:"+description);
formData.append("latitude", lat); //outputs correctly
formData.append("longitude", lng);//outputs correctly
formData.append("description", description); //outputs correctly
console.log(formData.get("latitude")+" "+formData.get("longitude")+" "+formData.get("description"));

downloadUrl(formData, function(data, responseCode) {
            console.log("RESP:"+responseCode);
            console.log(data.length);
            if (responseCode === 200  && data.length > 1) {
                var markerId = getMarkerUniqueId(lat,lng); // get marker id by using clicked point's coordinate
                var manual_marker = markers[markerId]; // find marker
                manual_marker.setIcon(purple_icon);
                infowindow.close();
                infowindow.setContent("<div style=' color: purple; font-size: 25px;'> Waiting for admin confirm!!</div>");
                infowindow.open(map, manual_marker);

            }else{
                console.log("RESPONSECODE: "+ responseCode); //419
                console.log("DATA: "+data); //html data with title of Page Expired
                infowindow.setContent("<div style='color: red; font-size: 25px;'>Inserting Errors</div>");
            }
        });

   function downloadUrl(formData, callback) 
   {

        var xhttp = new XMLHttpRequest;
        xhttp.onreadystatechange = function() {
            console.log(xhttp.readyState);
            if (xhttp.readyState == 4) {
                callback(xhttp.responseText, xhttp.status);
            }
        };

        xhttp.open("POST", "http://localhost/cacheFinder/public/save");
        xhttp.setRequestHeader('X-CSRF-TOKEN', '{{ csrf_token() }}');
        xhttp.send(formData);
    }

这是我的路线Route::post('save', 'MarkersController@save');

这是保存功能:

 public function save(Request $request){

$lat = $request->input("latitude");
$lng = $request->input("longitude");
$description = $request->input("description");

Marker::create([

        'latitude'=>$lat,
        'longitude'=>$lng,
        'description'=>$description
    ]);


   return redirect('usermap');
  }

任何帮助将非常感激

标签: javascriptphplaravelgoogle-maps

解决方案


将此添加到您的VerifyCsrfToken中间件

protected $except = [
    '/cacheFinder/public/*'
];

或者

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.setRequestHeader('X-CSRF-TOKEN',window.Cookies.get('_csrf'));


推荐阅读