javascript - How to pass parameters in GET requests with jQuery

ID : 10458

viewed : 30

Tags : javascriptjqueryajaxgetjavascript

Top 5 Answer for javascript - How to pass parameters in GET requests with jQuery

vote vote

91

Use data option of ajax. You can send data object to server by data option in ajax and the type which defines how you are sending it (either POST or GET). The default type is GET method

Try this

$.ajax({   url: "ajax.aspx",   type: "get", //send it through get method   data: {      ajaxid: 4,      UserID: UserID,      EmailAddress: EmailAddress   },   success: function(response) {     //Do Something   },   error: function(xhr) {     //Do Something to handle error   } }); 

And you can get the data by (if you are using PHP)

 $_GET['ajaxid'] //gives 4  $_GET['UserID'] //gives you the sent userid 

In aspx, I believe it is (might be wrong)

 Request.QueryString["ajaxid"].ToString();  
vote vote

80

Put your params in the data part of the ajax call. See the docs. Like so:

$.ajax({     url: "/TestPage.aspx",     data: {"first": "Manu","Last":"Sharma"},     success: function(response) {         //Do Something     },     error: function(xhr) {         //Do Something to handle error     } }); 
vote vote

76

Here is the syntax using jQuery $.get

$.get(url, data, successCallback, datatype) 

So in your case, that would equate to,

var url = 'ajax.asp'; var data = { ajaxid: 4, UserID: UserID, EmailAddress: EmailAddress }; var datatype = 'jsonp';  function success(response) { // do something here  }  $.get('ajax.aspx', data, success, datatype) 

Note $.get does not give you the opportunity to set an error handler. But there are several ways to do it either using $.ajaxSetup(), $.ajaxError() or chaining a .fail on your $.get like below

$.get(url, data, success, datatype)  .fail(function(){ }) 

The reason for setting the datatype as 'jsonp' is due to browser same origin policy issues, but if you are making the request on the same domain where your javascript is hosted, you should be fine with datatype set to json.

If you don't want to use the jquery $.get then see the docs for $.ajax which allows room for more flexibility

vote vote

66

Try adding this:

$.ajax({     url: "ajax.aspx",     type:'get',     data: {ajaxid:4, UserID: UserID , EmailAddress: encodeURIComponent(EmailAddress)},     dataType: 'json',     success: function(response) {       //Do Something     },     error: function(xhr) {     //Do Something to handle error     } }); 

Depends on what datatype is expected, you can assign html, json, script, xml

vote vote

56

The data property allows you to send in a string. On your server side code, accept it as a string argument name "myVar" and then you can parse it out.

$.ajax({     url: "ajax.aspx",     data: [myVar = {id: 4, email: 'emailaddress', myArray: [1, 2, 3]}];     success: function(response) {     //Do Something     },     error: function(xhr) {     //Do Something to handle error     } }); 

Top 3 video Explaining javascript - How to pass parameters in GET requests with jQuery

Related QUESTION?