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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7544160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:59:15  来源:igfitidea点击:

How to send parameters with jquery $.get()

ajaxjqueryget

提问by Dan Dinu

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

这似乎不起作用;当我使用时,我的 servlet 中得到一个空值request.getParameter("option")

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

如果我在浏览器中输入链接http://www.myite.com/manageproducts.do?option=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.

仍然得到相同的结果。

回答by Darin Dimitrov

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

如果您说它适用于直接manageproducts.do?option=1在浏览器中访问,那么它应该适用于:

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

as it would send the same GET request.

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

回答by daryl

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.

此外,您的示例代码中存在一些错误,不确定这是导致错误的原因还是输入问题时的拼写错误。

回答by Mikeys4u

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 语句中使用它。

回答by Akash

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 call - > GET http://localhost:3000/api.php?client=mikescafe

Good Luck.

祝你好运。