我最近尝试使用 jQuery 从 URL 获取一些响应。因此,我复制了 jQuery API Get Request Tutorial 的获取请求示例进入我的项目并尝试运行它,但我的调试消息告诉我,它无法继续运行。我使用一个简单的请求尝试了 javascript Ajax 库,但没有成功。
所以我问你,你是否能以某种方式帮助我。
这就是我所做的一切,但没有任何回应。
var url = "http://www.google.com";
$.get(url, function(data){
alert("Data Loaded: " + data);
});
我是否可能忘记包含 ajax 或 jQuery 库。为了更好地理解,我有 c 和 obj-c 经验,这就是我认为缺少库的原因。
在每个示例中,只有一个简短的 url,如“test.php”。我的完整 HTTP 网址是否有误?
感谢您提前回答。
Br 尼克
最佳答案
我提供了一个示例场景来帮助您入门:
<!-- Include this jQuery library in your HTML somewhere: -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script
这可能最好包含在外部 JS 文件中:
//Listen when a button, with a class of "myButton", is clicked
//You can use any jQuery/JavaScript event that you'd like to trigger the call
$('.myButton').click(function() {
//Send the AJAX call to the server
$.ajax({
//The URL to process the request
'url' : 'page.php',
//The type of request, also known as the "method" in HTML forms
//Can be 'GET' or 'POST'
'type' : 'GET',
//Any post-data/get-data parameters
//This is optional
'data' : {
'paramater1' : 'value',
'parameter2' : 'another value'
},
//The response from the server
'success' : function(data) {
//You can use any jQuery/JavaScript here!!!
if (data == "success") {
alert('request sent!');
}
}
});
});
关于javascript - jQuery 获取 HTTP URL 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5942105/