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
14
  • 3
    If you want to use GET why are you using POST? Commented Apr 4, 2018 at 16:13
  • 1
    userid is in the current URL? Really unclear Commented Apr 4, 2018 at 16:14
  • 1
    You could get a substring from window.location.href? It is very unclear as to what your goal is here... Commented Apr 4, 2018 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 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. Read How to Ask a good question. Be sure to take the tour Commented Apr 4, 2018 at 16:16
  • 1
    Possible duplicate of How to pass parameters in GET requests with jQuery Commented Apr 4, 2018 at 19:48

3 Answers 3

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
});
Sign up to request clarification or add additional context in comments.

1 Comment

Please check this: JQuery ajax
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>

Comments

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).

1 Comment

This is if you are trying to POST info received from the GET

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.