url:是指要导入文件的地址。 data:可选参数;因为Load不仅仅可以导入静态的html文件,还可以导入动态脚本,例如PHP文件,所以要导入的是动态文件时,我们可以把要传递的参数放在这里。 callback:可选参数;是指调用load方法并得到服务器响应后,再执行的另外一个函数。II. The second parameter is a string which specifies the status of the call, e.g. "success" or "error".
III. XMLHttpRequest object used to perform the AJAX call. It will contain properties which you can use to see what went wrong and many other things.
$("#**").load("no-content.html", function(responseText, statusText, xhr){
if(statusText == "success") alert("Successfully loaded the content!");
if(statusText == "error") alert("An error occurred:" + xhr.status + "-" + xhr.statusText);
});
2. get(), post()
I. get()
$.get("content.html", function(data, textStatus){
//the first parameter is the url, the second parameter is a callback function.
//for the callback function, the first parameter is the content of the page requested,
while the second callback parameter is the textual status of the request.
});
- GET 请求可被缓存
- GET 请求保留在浏览器历史记录中
- GET 请求可被收藏为书签
- GET 请求不应在处理敏感数据时使用
- GET 请求有长度限制
- GET 请求只应当用于取回数据
II. post()
$.post(URL, data, callback);
for example:
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "lc",
city: "beijing"
},
function(data,status){
});
});
3. Same Origin Policy - security feature
prevents us from making AJAX requests to a different domain or subdomain than the one currently executing the script.
4. JSONP ?
5. Showing Progress
Example:
<input type="button" name="btnDoRequest" value="Perform calculation" onclick="PerformCalculationWithImageProgress();" />
<img src="/images/ajax-loader.gif" style="display: none;" id="imgProgress" />
<script type="text/javascript">
function PerformCalculationWithImageProgress()
{
$("#imgProgress").show();
$.get("**.php", function(data, textStatus){
$("#imgProgress").hide();
alert(data);
}).error(function(){
$("#imgProgress").hide();
});
}
</script>
6. Aborting an AJAX request
Prevents making multiple AJAX callback functions
Example:
<input type="button" name="btnDoRequest" value="Start" onclick="PerformAbortableCalculation();"/>
<script type="test/javascript">
var calculationRequest = null;
function PerformAbortableCalculation()
{
if(calculationRequest != null){
calculationRequest.abort();
}
calculationRequest = $.get("**.php", function(data, status){
alert(data);
});
}
</script>
No comments:
Post a Comment