weixin_33721427 2019-10-22 16:27 采纳率: 0%
浏览 31

通过ajax问题传递数据

I'm trying to pass data to my laravel controller function via Ajax. At this point I just want to return the data being sent to verify ajax is working. I can "GET" with ajax, but when I try to "POST" ajax brakes.

Could someone please tell me what I'm doing wrong? Thank you.

Here is my ajax code...

var startMapLocation = { startCity: "Cleveland", startStat: "Oh" };
         $.ajax({
            type: "POST",
            url: url,
            data: startMapLocation,
            success: function(data, status) {
                //alert(data);
                console.log("success:", data);
            },
            error: function() {
                alert("Ajax Broke!" + status);
            }
        });

My laravel function is...

public function postphp( Request $request)
    {
         $a = $request->all();

         $city = $a["startCity"];

        return json_encode( $city );
    }
  • 写回答

1条回答 默认 最新

  • weixin_33724659 2019-10-23 14:38
    关注

    Thanks every one for your help. To resolve this issue, I first had to verify that my route was a post route not a get route.

    Route::post('/postphp', 'GSResultController@postphp');

    I also need to get my csrf-token and add it to the ajax call.

    headers: {
                    "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content")
                },
    

    This fixed my problem.

    评论

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表