Appem
Appem

Reputation: 303

how can i return view using ajax in laravel?

i got some problem here, i would like to chek my data when i post it using ajax here is my 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'
})

and then here is my web route

web.php

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

and here we are at my controller

controller.php

public function saveData(Request $request){

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

first i want to go to my succes.blade.php i dont know why my page never go anywhere event tough my post method is alright

enter image description here

seconds, i want to look at my data so its posible to use return dd($request) how can i solve it?

i were tried to use this

return (String) view(CrewProgram.succes) and return view(CrewProgram.success)->render(), before

and nothings works so help me someone, im using laravel

Upvotes: -1

Views: 61

Answers (2)

Kenneth Sunday
Kenneth Sunday

Reputation: 895

First you need to fix your AJAX data

from your version

$.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'
})

to this

$.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
}

})

Now we'll go to your Controller method

from this public function saveData(Request $request){

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

to this

public function saveData(Request $request){

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

}

can you create this PageRequest.php file to your app\Http\Requests folder

<?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 [
            //
        ];
    }
}

then instead of

public function saveData(Request $request){}

you make it

public function saveData(PageRequest $request){}

then check the value of $request->all() by doing dd($request->all())

Upvotes: 1

Raw Scripter
Raw Scripter

Reputation: 113

You can try this way. update your controller like this.

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

and set your 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)
     }
    })

Upvotes: 0

Related Questions