0

I can't seem to get an ajax call to work. The ajax post request is supposed to do some processing and return a success message. I have removed all the codes for processing and returning just the success message yet nothing is returned on the console. This is my code

My View

function payWithPaystack(){
        var handler = PaystackPop.setup({

            },
            callback: function(response){
                alert('success. transaction ref is ' + response.reference);
                    var userId = '<?php echo $userId; ?>';
                    console.log(userId);

                    $.post('/payment/'+userId, {psid: userId}, function (data) {
                        console.log(data);
                    })

               }
        });

My route

Route::match(['post','get'], '/payment/{userId}', 'PaymentController@index');

My controller

public function index(Request $request, $userId){
        $method = $request->isMethod('post');
        if($method){
            $message='User disabled!!';
            $success='Disabled';
            echo json_encode(array('message'=>$message,'success'=>$success));

        }else{
            return view('payment', compact('userId'));
        }
    }

Please what am I doing wrong here?

Edit: When I took the ajax outside the callback, it works so I am guessing the callback ain't allowing the ajax call. How do I resolve this?

shekwo
  • 1,411
  • 1
  • 20
  • 50
  • check the network tab and look for the ajax request to see a) whether it took place and b) if it did, what the actual response (i.e the status code and any content) from the server was. Maybe there's an error. – ADyson Sep 06 '18 at 12:15
  • @ADyson I saw “cancelled” – shekwo Sep 06 '18 at 12:28
  • [Download Postman](https://www.getpostman.com/apps) and make a request to your endpoint. Please inspect the response code and let us know what it is and the contents of the response body. Also, why are you passing a string containing PHP in your request? – Guybrush Sep 06 '18 at 12:44
  • Use `return` instead of `echo` – Noel Kriegler Sep 06 '18 at 13:08
  • @Lionel It returns a response on postman – shekwo Sep 06 '18 at 13:11
  • @NoelKriegler Still returns no response – shekwo Sep 06 '18 at 13:15
  • "cancelled"...nothing more than that? I would have hoped for a bit more info. How are you triggering the request? i.e. I mean what causes your payWithPaystack() function to run. [Here](https://stackoverflow.com/questions/7577275/jquery-ajax-requests-are-getting-cancelled-without-being-sent) is a long thread about possible reasons why ajax requests might be being cancelled. unfortunately without a bit more info it's hard to be sure why this might be happening. See if any of the answers there tally with your experience or your setup. – ADyson Sep 06 '18 at 13:50
  • @ADyson payWithPaystack() is called onload() of the body i.e. – shekwo Sep 06 '18 at 14:09
  • Dear all, When I took the ajax outside the callback, it works so I am guessing the callback ain't allowing the ajax call. How do I resolve this? – shekwo Sep 06 '18 at 14:24
  • You need to use Developer Tools in Chrome to check whether the request is actually being made to the server; you can use the Network tab of developer tools to do this. If you see a request is being sent and a response returned, take a screenshot of the response in the Network tab (try to capture the response body, response code etc.) and add it as an edit to your question. If the Network tab of developer tools doesn't show that a request is being made, inspect the console and ensure there are no errors being thrown (indicating some sort of syntax/logical error in your Javascript). – Guybrush Sep 08 '18 at 09:18

0 Answers0