简体   繁体   中英

HTTP GET request with JQuery

First of all, I am VERY new with JS so sorry for the simple question. 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.

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. 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). The url you are using should receive GET requests and answer something (in this case, "success" if "param" is passed), check it before proceeding.

$(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!');
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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