简体   繁体   English

使用JQuery的HTTP GET请求

[英]HTTP GET request with JQuery

First of all, I am VERY new with JS so sorry for the simple question. 首先,我对JS非常陌生,非常抱歉这个简单的问题。 I have a search box in a webpage which the user should type some input into it. 我在网页中有一个搜索框,用户应在其中输入一些输入内容。 After that, I need to perform a HTTP GET request to some address (say: http://m.mywebsite.com/AppService.svc/searchitems?param=userValue when the param parameter is the user input (in the below sample it is John Doe). So far I tried with this way, but nothings happen when I click somewhere in the webpage. Hope some of you can explain me how to do it right. 之后,我需要对某个地址执行HTTP GET请求(例如,当param参数是用户输入时, http//m.mywebsite.com/AppService.svc/searchitems? param = userValue(在下面的示例中) (John Doe)。到目前为止,我已经尝试过这种方法,但是当我单击网页中的某个位置时,什么都没有发生。希望你们中的一些人可以向我解释正确的做法。

Thanks 谢谢

$(document).ready(function(){

    document.addEventListener("click", function(e) {
        $.ajax({
            'url' : 'http://m.mywebsite.com/AppService.svc/searchitems',
            'type' : 'GET',
            'data' : {
                'param' : 'John Doe'
            },
            'success' : function(data) {
                if (data == "success") {
                    alert('request sent!');
                }
            }
        });
    });
});

I think you can't use addEventListener() function with document object. 我认为您不能将addEventListener()函数与document对象一起使用。 You should use something like that: 您应该使用类似这样的东西:

document.getElementById("myBtn").addEventListener("click", function(){
    document.getElementById("demo").innerHTML = "Hello World";
});

from here 这里

this works (if the 'url' is correct and accept GET requests). 这行得通(如果'url'是正确的并接受GET请求)。 The url you are using should receive GET requests and answer something (in this case, "success" if "param" is passed), check it before proceeding. 您正在使用的url应该会收到GET请求并回答一些问题(在这种情况下,如果“ param”已通过,则为“成功”),请在继续操作之前进行检查。

$(document).ready(function() {
    $(document).click(function() {
        $.ajax({
            url : 'http://m.mywebsite.com/AppService.svc/searchitems',
            type : 'GET',
            data : { param : 'John Doe' }
        }).done(function(data) {
            if (data == "success") alert('request sent!');
        });
    });
});

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

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