3

Currently I'm trying to pull some data via ajax and I'm not getting the data to appear properly.

In my ajax call I have this:

$.ajax({
      url:"{{ route('pricing.fetch') }}",
      method:"POST",
      data:{select:select, value:value, _token:_token, dependent:dependent, productId:productId},
      success:function(result)
      {

      $("ul[data-dependent='quantity']").html(result);

This works as expected. The problem is I'm trying to return data from different tables in my db. So I'm trying to do it by changing my result in ajax to this.

$("ul[data-dependent='quantity']").html(result.productQuantities);

The reason for me wanting to do this is because I have multiple drop downs I need. So I would also like to do another one like this:

$("ul[data-dependent='quantity']").html(result.productPaperStock);

my controller code is like this:

   $data = Product::with(['productQuantity', 'productPaperstock'])->where('ID', $productId)->first();
   // pull the quantity for this product
   $productQuanties = $data->productQuantity;
   $productPaperStock = 'hello';

   $output = '';

   foreach($productQuanties as $productQuantity)
   {
    $output .= "<li><span>" . $productQuantity->quantity_name . "</span></li>";
   }
    return response()->json["productQuanties" => $productQuanties, "productPaperStock" => $productPaperStock]);

I'm not sure what I'm doing wrong but using this example above I get a 500 error.

6
  • When you use method: 'post' the system will expect you to send some data... ajax on jquery uses method: 'get' by default, implicit. Could you try and see what happens? Or better, replace method: 'post' by dataType: 'json' Commented Aug 16, 2018 at 22:15
  • @Rafael yes I'm posting some data, I just didn't include in the code as it wasn't relevant to getting data form controller. I'll add it now thou. Commented Aug 16, 2018 at 22:23
  • I think I didn't make myself clear, sorry. By POST the system will expect data to be saved. If that's not what you want, stick to GET, where you are still allowed to send data as parameters. Commented Aug 16, 2018 at 22:35
  • @Rafael changed to dataType: 'json and changed route in routes to get instead of post. Still getting 500 error Commented Aug 16, 2018 at 22:59
  • @Rafael Also the reason why I'm using post is because I'm sending some parameters to the server to check for specific data i need returned. Commented Aug 16, 2018 at 23:13

1 Answer 1

2

You need to set dataType: json option in your ajax request, and then in your controller, you can return json response.

Also, you are missing the starting brace in your controller code. The correct code is

return response()->json(["productQuanties" => $productQuanties, "productPaperStock" => $productPaperStock])

(Note that ...storage/logs/laravel.log is an awesome place to get insights into what's screwing your app:))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.