gnikyt / laravel-shopify

A full-featured Laravel package for aiding in Shopify App development
MIT License
1.24k stars 374 forks source link

AJAX call returns HTML content and 302 warning #977

Closed Nagdev-Savita closed 3 years ago

Nagdev-Savita commented 3 years ago

Expected Behaviour

While calling controller through ajax from blade file it have to return product data in console

Current Behavior

In ajax call html content is returned

Failure Information

I have tried to remove middleware from route then it is working fine. But after that i am not able to get product data in controller.

Steps to Reproduce

Please provide detailed steps for reproducing the issue.

  1. Step 1: My blade file code:
  $(document).ready(function() {
    $(".add").click(function(e){
      e.preventDefault();
      $.ajax({
          type:'GET',
          url:"{{ route('get.products') }}",
          success:function(data){
            console.log(data);
          }
      });
    });
  });
  1. Step 2: My route for controller Route::get('get-product',[App\Http\Controllers\AjaxController::class,'getProducts'])->middleware(['verify.shopify'])->name('get.products');
  2. Step 3: My controller code:
public function getProducts(Request $request)
{
        $shop=Auth::user();
        $products=$shop->api()->rest('GET','/admin/api/2021-07/products.json',['fields'=>'id,title']);
        $products=$products['body']['container']['products'];       
        return $products;
    }

Context

burfbari commented 3 years ago

@Nagdev-Savita can you please paste here the requests that you see in your Console?

Nagdev-Savita commented 3 years ago

@burfbari It returns whole designing of current page in console. Including each and every tag of current page.

burfbari commented 3 years ago

@burfbari It returns whole designing of current page in console. Including each and every tag of current page.

Did you check the seesion token in your headers?

Nagdev-Savita commented 3 years ago

@burfbari Sorry, But i can't get exactly what you are asking to check. I had checked request of AJAX route in network and i didn't get anything as session token.

Kyon147 commented 3 years ago

@Nagdev-Savita

From looking at your code you are not returning json from your controller back to the AJAX call and you are not setting the content type for the ajax call as JSON. These are the usual suspects for returning the HTML page.

https://stackoverflow.com/questions/46600395/ajax-response-returns-html-content/46600478

As this is not a package bug I am closing this ticket.