Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

520
Views
Laravel: return data from server without redirect

What I'm trying to do is call function form server and validate data in javascript function but the return statement from server make redirect before response.complete("success")

button.onclick = async function handlePurchase() {
  const payment = new PaymentRequest(methods, details, options);
  try {
    const response = await payment.show();
    // Call server logic here and validate response if OK continue 
    // But server response redirect here so code not completed
    $('#checkout-form').submit();
    $.ajax({
    url: '{{route("apple-pay")}}',
      type: 'post',
      data: $('#checkout-form').serialize(),
      success: function(data){
          console.log('data :>> ', data);
      }
    });  
    await response.complete("success");
    // redirect to success page
  } catch (err) {
    console.error("Uh oh, something bad happened", err.message);
  }
}

Server function:

    public function pay(Request $request)
    {
        $merchant_id = env('CREDIMAX_MERCHANT_ID');
        $password = env('CREDIMAX_INTEGRATION_PASSWORD');

        $response = Http::withBasicAuth('merchant.'.$merchant_id, $password)->put
        ('https://example.com/api/rest/merchant/'.$merchant_id.'/order/1530/transaction/1', $data);

        return $respone->json();

     }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Seperate the request you are making to the third party app and the response you are sending back to you ajax call. This is how I mean:

public function pay(Request $request)
    {
        $merchant_id = env('CREDIMAX_MERCHANT_ID');
        $password = env('CREDIMAX_INTEGRATION_PASSWORD');

        $response = Http::withBasicAuth('merchant.'.$merchant_id, $password)->put
        ('https://example.com/api/rest/merchant/'.$merchant_id.'/order/1530/transaction/1', $data);

        return response()->json([
          'success' => $response->ok() ? 1 : 0,
          ...
        ]);

     }
over 4 years ago · Santiago Trujillo Report

0

Check the last line in the controller it says "return $respone->json();" and should be "return $response->json();" -- missing the "s" in response.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!