【问题标题】:how can i return view using ajax in laravel?如何在 laravel 中使用 ajax 返回视图?
【发布时间】:2020-07-21 01:27:54
【问题描述】:

我在这里遇到了一些问题,我想在使用 ajax 发布数据时检查我的数据 这是我的ajax

ajax.js

let dataNewMemoData = JSON.stringify(createnewMemoData);

 $.ajax({
url: '/crew_memo/submitdata',
method: 'POST',
dataType: 'json',
contentType: 'json',
data: dataNewMemoData,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
contentType: 'application/json; charset=utf-8'
})

然后这是我的网络路线

web.php

 Route::post('/crew_memo/submitdata', 'CrewProgramController@saveData');

我们在我的控制器那里

controller.php

public function saveData(Request $request){

    $request = json_decode($request->getContent());
    return view('CrewProgram.succes')->render();
    // return dd($request);
}

首先我想去我的succes.blade.php我不知道为什么我的页面永远不会去任何地方事件艰难我的发布方法是好的

秒,我想查看我的数据,以便可以使用return dd($request) 我该如何解决?

我试过用这个

return (String) view(CrewProgram.succes)return view(CrewProgram.success)->render(),之前

什么都没有,所以请帮助我,我正在使用 laravel

【问题讨论】:

  • 在发送 post 请求后,你收到回调了吗?你应该抓住回调并更新 dom。
  • 对不起,回调是什么意思?我是初学者
  • 在标题中指定 'Accept': 'application/json','Content-Type': 'text/plain' 并再次检查。
  • 发出发布请求后。它会返回您的视图代码。现在在您的 javascript 代码中,您必须在成功函数中捕获它,然后您可以更新 dom html

标签: ajax laravel post httprequest


【解决方案1】:

首先你需要修复你的 AJAX 数据

来自你的版本

$.ajax({
url: '/crew_memo/submitdata',
method: 'POST',
dataType: 'json',
contentType: 'json',
data: dataNewMemoData,
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
contentType: 'application/json; charset=utf-8'
})

到这里

$.ajax({
url: '/crew_memo/submitdata',
method: 'POST',
contentType: 'json',
data: { content : dataNewMemoData},
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
success : function(data){
   console.log(data) //this will be your html respone make sure you don't have any html or header or body tag inside your view file you want to render.
},
error : function(jqXHR,textStatus,thrownError){
   console.log(jqXHR) //for non 2xx or 3xx response code
}

})

现在我们将转到您的 Controller 方法

从此 公共函数 saveData(Request $request){

    $request = json_decode($request->getContent());
    return view('CrewProgram.succes')->render();
    // return dd($request);
}

到这里

public function saveData(Request $request){

        $request = json_decode($request->get('content));
        return view('CrewProgram.succes')->render();
        // return dd($request);

}

您能否将此PageRequest.php 文件创建到您的app\Http\Requests 文件夹中

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PageRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            //
        ];
    }
}

然后代替

public function saveData(Request $request){}

你做到了

public function saveData(PageRequest $request){}

然后通过dd($request-&gt;all())检查$request-&gt;all()的值

【讨论】:

  • 创建我发送的 PageRequest.php 文件
  • 好像你在使用 Request 全局类。
  • 或者如果您在 ajax 中使用此方法感到舒适:data: dataNewMemoData,没关系,只需删除 ajax 中那些不必要的键
  • 我得到了错误,它说 pageRequest 不存在imgur.com/yx6bEwV 甚至很难我已经添加它imgur.com/2u6rBIS
  • 为什么我不应该使用全局请求类?
【解决方案2】:

你可以试试这个方法。像这样更新你的控制器。

       return response()->json([
            'htmlData' => view('CrewProgram.succes')->render()
        ]);

并设置你的 jquery

 $.ajax({
    url: '/crew_memo/submitdata',
    method: 'POST',
    dataType: 'json',
    contentType: 'json',
    data: dataNewMemoData,
    headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
    contentType: 'application/json; charset=utf-8',
    success: function (data) {
       alert(data.htmlData)
     }
    })

【讨论】:

  • 我收到了警报,但是我的页面没有更改为 CrewProgram.succes,我如何检查我的数据是否存在,你知道我需要检查我的数据是否已经在控制器上使用 dd()
猜你喜欢
  • 2020-01-06
  • 2013-11-10
  • 2015-04-22
  • 2023-03-28
  • 2019-12-19
  • 2021-01-14
  • 2016-11-23
  • 1970-01-01
  • 2013-12-09
相关资源
最近更新 更多