简体   繁体   English

使用jQuery / ajax进行GET请求

[英]using jQuery/ajax for GET request

I'm trying to use jQuery to send an input to be processed on another page but I'm having some trouble getting it to work. 我正在尝试使用jQuery发送输入以在另一个页面上处理,但我在使其工作时遇到一些麻烦。

When I open up the network console I can see a GET method set to common_functions, but there's no variables attached to it. 当我打开网络控制台时,我可以看到设置为common_functions的GET方法,但没有附加变量。 I'm getting this 我得到了这个

http://localhost/project/common_functions.php

instead of something like 而不是像

http://localhost/project/common_functions.php?name=abcdef

Here's my HTML. 这是我的HTML。 It's fine as far as I know because I get an alert from the function receiving the input 据我所知,这很好,因为我收到来自接收输入的函数的警报

<form method="post">
<table>
<tr><td>Project Name</td><td><input type = "textbox" name="project_name" id = "project_name" onkeyup = "get_name(this.value);" /></td></tr>
<tr><td>Project Description</td><td><input type = "textbox" name="project_name" id = "project_description" onkeyup = "get_description(this.value);"/></td></tr>
<tr><td><input type="submit" name="submit_project"></td></tr>
</table>
</form> 

Here's the jQuery. 这是jQuery。 I'm attempting to send a GET request. 我正在尝试发送GET请求。

function get_name(name){
    var project_name = name;
    $.get('common_functions.php', function(name) {
        alert(project_name); //this displays the name
        $('#project_name').html(name);
    });
}

When I have just this I get an alert, so I know it's at least acknowledging the onkeyup. 当我有这个时,我得到一个警报,所以我知道它至少承认了onkeyup。

You are not sending any data. 您没有发送任何数据。 You can either append the data to the script name or send it as key - value pairs: 您可以将数据附加到脚本名称或将其作为键 - 值对发送:

$.get('common_functions.php', {'project_name': name}, function(data) {

And I would not use the same variable name in the callback function, that only leads to confusion. 我不会在回调函数中使用相同的变量名,这只会导致混淆。

What you are expecting to see is if you called your get function with the following parameter. 您期望看到的是使用以下参数调用get函数。

var url = 'common_functions.php?project_name=' + Name;

$.get(url , function(name) {
    alert(project_name); //this displays the name
    $('#project_name').html(name);
});

The second parameter is just the return function, this is what will get called when your call to common_functions.php returns. 第二个参数只是返回函数,当你调用common_functions.php返回时,这将被调用。

The nicest way to do it is as @jeroen has shown. 最好的方法就是@jeroen所展示的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM