James
James

Reputation: 557

how to send and get query parameters in jquery

Hi I am quite new to jquery and web programming in general.

My question is how to send hidden query parameters using a post request in jQuery and Retreive the data from the post request in another. I know that there are a lot of tutorials on .post in jQuery but I cant seem to find any on .get requests (if that is what I need here)

For example in one .js file for one page I have

$.ajax({
    type: 'POST',

    url: 'url',
    data: {
       'startDate': this.options.startDate,
       'endDate': this.options.endDate,
       'selectedReport': this.options.endDate,
    },
    success: function (msg) {
       alert('wow' + msg);
    }
});

but in another js file for another page I want to have like a get request that retrieves these parameters.

Could anyone explain how would I write this get request in the js file to retrieve them?

Thank you for the help

Upvotes: 0

Views: 825

Answers (4)

James
James

Reputation: 557

I realized that JavaScript post request like a form submit answers my questions because it is more client side although the ajax is server side. thank you for all of the help!

Upvotes: 1

Piotr
Piotr

Reputation: 197

It seems to me that POST data is data that is handled server side. And Javascript is on client side. So there is no way you can read a post data using JavaScript.

Upvotes: 2

Unnie
Unnie

Reputation: 947

.ajax() POST data is send as query string parameters. In the other page you can write javascript to fetch these query string values.Below is sample to read query string values:

(function ($) {
            $.QueryString = (function (a) {
                if (a == "") return {};
                var b = {};
                for (var i = 0; i < a.length; ++i) {
                    var p = a[i].split('=');
                    if (p.length != 2) continue;
                    b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
                }
                return b;
            })(window.location.search.substr(1).split('&'))
        })(jQuery);

You can the above function like below:

var startDate=$.QueryString["startDate"];

https://api.jquery.com/jQuery.ajax/

Upvotes: 1

Nephelococcygia
Nephelococcygia

Reputation: 1131

With your current function you are sending POST data to the otherside. For example, in PHP, data sent will be in the $_POST array.

To set a GET request you just have to set type from POST to GET

type: 'GET'

Then on the PHP side data sent will be in $_GET array.

Upvotes: 1

Related Questions