[ajax] How to send parameters with jquery $.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.

This question is related to ajax jquery get

The answer is


I got this working : -

$.get('api.php', 'client=mikescafe', function(data) {
...
});

It sends via get the string ?client=mikescafe then collect this variable in api.php, and use it in your mysql statement.


This is what worked for me:

$.get({
    method: 'GET',
    url: 'api.php',
    headers: {
        'Content-Type': 'application/json',
    },
    // query parameters go under "data" as an Object
    data: {
        client: 'mikescafe'
    }
});

will make a REST/AJAX call - > GET http://localhost:3000/api.php?client=mikescafe

Good Luck.


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.


Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to get

Getting "TypeError: failed to fetch" when the request hasn't actually failed java, get set methods For Restful API, can GET method use json data? Swift GET request with parameters Sending a JSON to server and retrieving a JSON in return, without JQuery Retrofit and GET using parameters Correct way to pass multiple values for same parameter name in GET request How to download HTTP directory with all files and sub-directories as they appear on the online files/folders list? Curl and PHP - how can I pass a json through curl by PUT,POST,GET Making href (anchor tag) request POST instead of GET?