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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)