如何使用jquery $.get()发送参数

时间:2022-12-08 20:01:46

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

我正在尝试jquery GET,我想发送一个参数。

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");

这似乎行不通;当我使用request.getParameter(“选项”)时,我在servlet中得到一个null。

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

如果我将链接输入到浏览器http://www.myite.com/manageproducts.do?选择= 1它完美的工作。

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.

仍然得到相同的结果。

4 个解决方案

#1


56  

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

如果你说它可以直接访问管理产品。选项=1在浏览器中,它应该与:

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

as it would send the same GET request.

因为它会发送相同的GET请求。

#2


9  

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.

您的示例代码中也有一些错误,不确定是这些错误导致了错误,还是输入问题时出现了错误。

#3


4  

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.

它通过获取字符串?client=mikescafe发送,然后在api中收集这个变量。php,并在mysql语句中使用它。

#4


0  

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

将进行REST/AJAX调用- >获取http://localhost:3000/api.php?client=mikescafe吗

Good Luck.

祝你好运。

#1


56  

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

如果你说它可以直接访问管理产品。选项=1在浏览器中,它应该与:

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

as it would send the same GET request.

因为它会发送相同的GET请求。

#2


9  

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.

您的示例代码中也有一些错误,不确定是这些错误导致了错误,还是输入问题时出现了错误。

#3


4  

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.

它通过获取字符串?client=mikescafe发送,然后在api中收集这个变量。php,并在mysql语句中使用它。

#4


0  

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

将进行REST/AJAX调用- >获取http://localhost:3000/api.php?client=mikescafe吗

Good Luck.

祝你好运。