0

maybe it seems easy, but I could not find any useful solution for it. Let's say I have an url with GET value of:

/?userid=1

How can I pass the value of 1 using AJAX? Also the userid changes depending on the link that has been pressed so it cannot be statically sent with AJAX.

$.ajax({
                    type: "POST",
                    url: "actions.php?action=getUserProfileData",
                    data: "userid=" + userid, // I am talking about this part
Rvfvl
  • 349
  • 4
  • 18
  • 3
    If you want to use GET why are you using POST? – Spencer Wieczorek Apr 04 '18 at 16:13
  • 1
    userid is in the current URL? Really unclear – epascarello Apr 04 '18 at 16:14
  • type: "POST", change to **type: "GET",** – Roy Bogado Apr 04 '18 at 16:15
  • I am also posting few other values using the same AJAX call, but I have rewritten it for question purpose just to focus on GET userid. – Rvfvl Apr 04 '18 at 16:15
  • 1
    You could get a substring from `window.location.href`? It is very unclear as to what your goal is here... – JO3-W3B-D3V Apr 04 '18 at 16:16
  • After changing to `GET` why not just append it to the url? Is that what you're trying? Please edit to explain more. – adprocas Apr 04 '18 at 16:16
  • is the userid a parameter from the url of your window? If yes, you could try something like https://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-get-parameters – mind Apr 04 '18 at 16:16
  • 2
    We are always glad to help and support new coders but *you need to help yourself first*. After [doing more research](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) if you have a problem **post what you've tried** with a **clear explanation of what isn't** working and provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Read [How to Ask a good question](https://stackoverflow.com/help/how-to-ask). Be sure to [take the tour](https://stackoverflow.com/tour) – adprocas Apr 04 '18 at 16:16
  • @melon210 You can't use both GET and POST on the same call, only one or the other. There is no reason why you would need to do that. – Spencer Wieczorek Apr 04 '18 at 16:16
  • @JO3-W3B-D3V window.location.href solved the issue. Thanks. – Rvfvl Apr 04 '18 at 16:19
  • I think this existing post answers your question. https://stackoverflow.com/questions/15576548/how-to-pass-parameters-in-get-requests-with-jquery – Abba Apr 04 '18 at 16:20
  • No problem @melon210, happy to help, I assumed that was what you were trying to do. – JO3-W3B-D3V Apr 04 '18 at 16:23
  • @SpencerWieczorek I don’t think that’s true. I’m pretty sure you could use both together. Anyway, I thing his question was about getting the value of the GET Param on this page, not posting it to another page – carboncannonball Apr 04 '18 at 16:28
  • 1
    Possible duplicate of [How to pass parameters in GET requests with jQuery](https://stackoverflow.com/questions/15576548/how-to-pass-parameters-in-get-requests-with-jquery) – max66 Apr 04 '18 at 19:48

3 Answers3

1

GET doesn't support sending data like POST or PUT, so you need to concatenate everything in the URL like you're already doing:

$.ajax({
       type: "GET",
       url: "actions.php?action=getUserProfileData&userid="+ userid
});
Daniel Maiochi
  • 607
  • 6
  • 16
1

Try this :)

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                $.ajax({
                    url: "https://jsonplaceholder.typicode.com/comments",
                    type: "get", 
                    data: {
                        postId: 1
                    },
                    success: function(response) {

                        console.log(response);
                    },
                    error: function(xhr) {

                        console.log(error);
                    }
                });
            });
        });
    </script>
</head>

<body>

    <button>Get Data</button>

</body>

</html>
Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29
0

I think functions will help you out.

function postAction(userid) {
  $.ajax({
    type: "POST",
    url: "actions.php?action=getUserProfileData",
    data: "userid=" + userid,
    success: function(data) {
      alert(data);
    }
  });
}
function getUser(id) {
  $.ajax({
    type: "GET",
    url: "URL.php?userid=" + id,
    success: function(data) {
      postAction(data.id)
    }
  });
}

getUser("1");

The functions make sure the variables are in scope when you need them (closure basically).

Ken Hansen
  • 131
  • 2