Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send parameters with jquery $.get()

Tags:

jquery

ajax

get

I'm trying to do a jquery GET and i want to send a parameter.

here's my function:

$(function() {     var availableProductNames;     $.get("manageproducts.do?option=1", function(data){         availableProductNames = data.split(",");;         alert(availableProductNames);         $("#nameInput").autocomplete({             source: availableProductNames         });     }); }); 

This doesn't seem to work; i get a null in my servlet when i use request.getParameter("option");

If i type the link into the browser http://www.myite.com/manageproducts.do?option=1 it works perfectly.

I also tried:

$.get(     "manageproducts.do?",     {option: "1"},     function(data){} 

which doesn't work either.

Can you please help me?

EDIT:

also tried

       $.ajax({       type: "GET",       url: "manageproducts.do",      data: "option=1",      success: function(msg){         availableProductNames = msg.split(",");         alert(availableProductNames);         $("#nameInput").autocomplete({         source: availableProductNames     });         }       }); 

Still getting the same result.

like image 831
Dan Dinu Avatar asked Sep 25 '11 07:09

Dan Dinu


People also ask

Can you send data in a GET request?

GET is an HTTP method for requesting data from the server. Requests using the HTTP GET method should only fetch data, cannot enclose data in the body of a GET message, and should not have any other effect on data on the server.

What is get in jQuery?

Description. The jQuery. get( url, [data], [callback], [type] ) method loads data from the server using a GET HTTP request. The method returns XMLHttpRequest object.


2 Answers

If you say that it works with accessing directly manageproducts.do?option=1 in the browser then it should work with:

$.get('manageproducts.do', { option: '1' }, function(data) {     ... }); 

as it would send the same GET request.

like image 128
Darin Dimitrov Avatar answered Sep 21 '22 18:09

Darin Dimitrov


Try this:

$.ajax({     type: 'get',     url: 'manageproducts.do',     data: 'option=1',     success: function(data) {          availableProductNames = data.split(",");          alert(availableProductNames);      } }); 

Also You have a few errors in your sample code, not sure if that was causing the error or it was just a typo upon entering the question.

like image 44
daryl Avatar answered Sep 23 '22 18:09

daryl