HTTP GET request with JQuery
HTTP GET request with JQuery
首先,我对JS很陌生,所以很抱歉这个简单的问题。我在网页中有一个搜索框,用户应该在其中输入一些内容。之后,我需要对某个地址执行HTTP GET请求(例如:当参数是用户输入时 http://m.mywebsite.com/AppService.svc/searchitems?param=userValue(在下面的示例中是John Doe)。到目前为止,我尝试了这种方式,但是当我单击网页中的某处时没有任何反应。希望你们中的一些人可以解释我如何正确地做到这一点。
谢谢
$(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!');
}
}
});
});
});
我认为您不能addEventListener()
函数与document
对象一起使用。你应该使用这样的东西:
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("demo").innerHTML = "Hello World";
});
从这里
这
有效(如果"url"是正确的并接受GET请求)。您使用的 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!');
});
});
});
相关文章:
- Using jQuery with classes from ES6
- setTimeout in jQuery with a loop
- JQuery with ajax 不断发布多个结果
- Ajax and jQuery with PHP
- jQuery with Google maps
- Lab.js 和 jQuery with $(window).load(function() 过早触发
- Using jQuery with Aurelia
- django.jQuery with django admin change_form.html
- jquery with WordPress loop
- jQuery with AJAX 只执行一部分代码
- JQuery with css3 keydown keyCode = 37 and 39
- Using Jquery with PHP
- Using jQuery with node.js
- JQuery with Element ID
- Using jquery with OO Javascript
- RE: implementing rest / jquery with JS
- Jquery with Asp.net button
- Load jQuery with RequireJS
- jQuery with Callback and Complete
- Joomla 2.5 jQuery with TinyMCE