0

I'm new at Laravel and I'm actively trying to code better, but I'm currently stuck with problems I don't know how to solve.

The controller :

public function sendGiving($contents){
    $redirectURL = $contents->redirectURL;
    var_dump($redirectURL); // the variable is available, logged in network section
    return View::make('giving/giving')->with('redirectURL', $redirectURL);
}

The view (on AJAX) :

function submitForm() {
    if (is_personal_data_complete() == true && is_offering_filled() == true && isreCaptchaChecked() == true) {
        var base_url = window.location.origin;
        
        //send ajax request    
        $.post("{{ route('send_giving') }}",
            {
                _method: 'POST',
                _token: '{{ csrf_token() }}',
                name: $('#txtName').val(),
                email: $('#txtEmail').val(),
                phone_number: $('#txtnohp').val(),
                thanksgiving_offerings: total_thanksgiving,
                tithe_offerings: total_tithe,
                firstborn_offerings: total_firstborn,
                build_offerings: total_build,
                deacon_offerings: total_deacon,
                mission_offerings: total_mission,
                paud_offerings: total_paud,
                dataType: "jsonp",
                async : false,
                success: function($redirectURL){
                    alert($redirectURL);
                },
            });
            
    }
       
    else if (is_personal_data_complete() == false) {
        alert("Please fill in your data form");
    }
    else if (is_offering_filled() == false) {
        alert("Please fill in your offerings");
    }
    else if (isreCaptchaChecked() == false){
        alert("Please check the captcha");
    }
    return false;
}

The alert always returns undefined though, what am I missing?

5 Answers5

0

The view() function just creates an instance of the View class. Not just an HTML string. For that you should call render():

$returnHTML = view('giving/giving')->with('redirectURL', $redirectURL)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
STA
  • 30,729
  • 8
  • 45
  • 59
Zaid Haider
  • 530
  • 6
  • 11
0

Please try this: return response()->json($redirectURL)

When you use Laravel and write API, you need to use this command to reponse JSON for frontend

0

When you return in your controller return View::make('giving/giving')->with('redirectURL', $redirectURL);

You are returning a VIEW file, which will be return as the body of the HTTP request.

and you are also passing to Your view file redirectUrl which will be accessible in your view file.

And when you perform your AJAX request, you are getting a response with a body which contain HTML/TEXT Content not JSON.

SO YOU CAN'T HAVE ACCESS TO redirectURL VARIABLE

So what you should do by the way is to return simple a JSON body by returning in your Controller something like

return response()->json([
    'redirectURL' => $redirectURL
]);

No need to return a VIEW FILE

You can't return in the same controller JSON data in the body and a VIEW FILE

Yves Kipondo
  • 5,289
  • 1
  • 18
  • 31
  • if im understanding right, this is just returning json var 'redirectURL' from body, but then how to access it on my ajax call ? Because it still undefined – Nicholas Kristian Darmawan Jul 17 '20 at 09:29
  • you're right, in your the `success` settings of the `ajax` function you can have access to the `JSON` return from your controller as parameter. You should just ensure that your ajax request are handled by the right controller and return the right `JSON` data. – Yves Kipondo Jul 17 '20 at 09:39
0

The main issue is here that you try to send a POST with JSONP data type. There are a lot of explanations on this on SO, e.g https://stackoverflow.com/a/18934048/8574551

Try to remove it and use smth like the next:

...
contentType: "application/json",
dataType: "json",
...

On another hand, you can omit these 2 parameters (check https://api.jquery.com/jquery.post/)

To return the data from the controller action you can use response()->json(..) (as described in other answers)

0

the problem is on the ajax request, as after changing the format it works nicely