在Laravel 5中通过AJAX从View到Controller发布数据

时间:2023-01-09 19:41:33

Data

I have a data

我有一个数据

var data = [];
data['upnp_enabled'] = $("#upnp-switch").val();

When I console.log(data); I got [upnp_enabled: "true"]

当我在console.log(数据);我得到了[upnp_enabled:“true”]


Ajax

I've tried to make a PUT to my Controller via AJAX

我试图通过AJAX对我的控制器进行PUT

var ajax = $.ajax({
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value')},
    url: '/' + currentCPE + '/vlan/' + currentPageVLAN + '/upnp',
    type: 'PUT',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: data
    //data: JSON.stringify(data) <---- I even tried this, same result !!
});
ajax.done(function (data) {
    parseMsgData(data, "Setting saved");
    console.log(data);
});

Controller

Then In my Controller, I have this

然后在我的控制器中,我有这个

public function update_upnp($cpe_mac, $vlan_id){

    $inputs = Input::all();
    dd($inputs);

}

I kept getting in my Network Tab > XHR

我一直在进入我的网络标签> XHR

[]

[]


I expect to see something like this for my dd($inputs);

我希望能为我的dd($输入)看到类似的东西;

[upnp_enabled: "true"]

[upnp_enabled:“true”]


Can someone please fill in what I missed ?

有人可以填写我错过的内容吗?

2 个解决方案

#1


1  

Ok. First the request should be something like (notice that data is a JSON and not an array):

好。首先请求应该是这样的(注意数据是JSON而不是数组):

var data = {};
data['upnp_enabled'] = $("#upnp-switch").val();

var ajax = $.ajax({
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value')},
    url: '/' + currentCPE + '/vlan/' + currentPageVLAN + '/upnp',
    type: 'PUT',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: data
    //data: JSON.stringify(data) <---- I even tried this, same result !!
});
ajax.done(function (data) {
    parseMsgData(data, "Setting saved");
    console.log(data);
});

In your controller you may need to have something like:

在您的控制器中,您可能需要具有以下内容:

use Illuminate\Http\Request;

public function update_upnp($cpe_mac, $vlan_id, Request $request){

    return $request->input();
}

Hope it helps!

希望能帮助到你!

#2


1  

in your controller you need the following

在您的控制器中,您需要以下内容

  1. make sure that you are using the Request Class. :

    确保您使用的是请求类。 :

    use App\Http\Requests;

    使用App \ Http \ Requests;

  2. you need to accept the Request through your method as an argument

    你需要通过你的方法接受请求作为参数

        public function update_upnp($cpe_mac, $vlan_id, Request $request){
          dd($request->attribute);
        }
    

Good Luck

祝你好运

#1


1  

Ok. First the request should be something like (notice that data is a JSON and not an array):

好。首先请求应该是这样的(注意数据是JSON而不是数组):

var data = {};
data['upnp_enabled'] = $("#upnp-switch").val();

var ajax = $.ajax({
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('value')},
    url: '/' + currentCPE + '/vlan/' + currentPageVLAN + '/upnp',
    type: 'PUT',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: data
    //data: JSON.stringify(data) <---- I even tried this, same result !!
});
ajax.done(function (data) {
    parseMsgData(data, "Setting saved");
    console.log(data);
});

In your controller you may need to have something like:

在您的控制器中,您可能需要具有以下内容:

use Illuminate\Http\Request;

public function update_upnp($cpe_mac, $vlan_id, Request $request){

    return $request->input();
}

Hope it helps!

希望能帮助到你!

#2


1  

in your controller you need the following

在您的控制器中,您需要以下内容

  1. make sure that you are using the Request Class. :

    确保您使用的是请求类。 :

    use App\Http\Requests;

    使用App \ Http \ Requests;

  2. you need to accept the Request through your method as an argument

    你需要通过你的方法接受请求作为参数

        public function update_upnp($cpe_mac, $vlan_id, Request $request){
          dd($request->attribute);
        }
    

Good Luck

祝你好运