0

I'm currently passing a laravel object's ID through a href click to a JS function

<a onclick="getDetails('{{$details->id}}')"</a>

And this works to some degree, because my console.log shows id 28 as it should. However, when sending the data in my ajax call my header shows 28: so it's sending the value as the key with a null value, which means my return is null.

How can I properly change this so that this correct value gets sent in my ajax call as ID:28

<script type="text/javascript">
  function getDetails(detailID) {
   console.log(detailID);
   $.ajax({
   url: "/details",
   data: detailID,
   _token: "{{ csrf_token() }}",
   type: "POST",
   success: function (data) {
   console.log(data);
   },
  });
  };
</script>
5
  • @TimLewis sorry, typo. just fixed it Commented Nov 13, 2019 at 17:04
  • No problem (guessed as much). Next issue, shouldn't data be an object? like data: {detailID: detailID},? Commented Nov 13, 2019 at 17:05
  • I believe that's correct, let me see if that's the issue Commented Nov 13, 2019 at 17:06
  • That was it! I don't know what I was thinking, but thank you! If you want to make an answer I'll accept it promptly Commented Nov 13, 2019 at 17:08
  • Sure thing :) Gimme a second Commented Nov 13, 2019 at 17:09

1 Answer 1

1

When sending data via $.ajax(), your data attribute needs to be a valid object (or similar):

$.ajax({
  url: "/details",
  data: { detailID: detailID },
  _token: "{{ csrf_token() }}",
  type: "POST",
  success: function (data) {
     console.log(data);
  },
  ...
});

There are other valid ways of specifying valid data, but data: detailID on its own is not one of them. Also, if you want this accessible as ID in Laravel (as initially suggested), simply change to:

data: { ID: detailID }
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.