IE7及以下請求方式用GET的話,URL的限制是個很容易忽視的問題(最大2083個字符)。所以如果URL有可能過長的話,一定要用POST。
--------------------------------------------------------------------------------
終止Ajax請求
終止請求需要調用XMLHttpRequest對象的abort()方法
而在jQuery中的$.get,$.post、$.ajax、$.getJSON、$.getScript...的返回值都是XMLHttpRequest對象.
調用abort()后,ajax請求立即停止,但仍然會執行success的回調函數
所以在success的回調函數中需要先判斷 ajaxGet 或 data是否存在,存在才執行回調函數
復制代碼 代碼如下:
var ajaxGet = $.get(someURL,someData,function(data){
if(!data)return true;
//TODO
});
ajaxGet.abort();
復制代碼 代碼如下:
function deal(data){
//TODO
}
var s= document.createElement("script")
//不必一定叫callback,但是一定要跟服務器端的Request.QueryString匹配
s.src = "http://172.20.2.60:8088/newwebsite/MyHandler.ashx?callback=func";
document.body.appendChild(s)
復制代碼 代碼如下:
<%@ WebHandler Language="C#" %>
using System;
using System.Web;
public class Test : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.Charset = "utf-8";
context.Response.ContentType = "text/javascript";
string callback = context.Request.QueryString["callback"];//回調函數名
string json = "{'name':'Ray','msg':'hello world!'}";//JSON格式的字符串
string result = string.Format("{0}({1})", callback, json);
context.Response.Write(result);
context.Response.Flush();
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
}
復制代碼 代碼如下:
$.ajax({
url: "http://172.20.2.60:8088/newwebsite/MyHandler.ashx"
, dataType: "jsonp"
, success: function(data) {
//TODO
}
});
復制代碼 代碼如下:
contentType
Default: 'application/x-www-form-urlencoded; charset=UTF-8'
When sending data to the server, use this content type.
Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases.
If you explicitly pass in a content-type to $.ajax(),
then it'll always be sent to the server (even if no data is sent).
If no charset is specified, data will be transmitted to the server using the server's default charset;
you must decode this appropriately on the server side.
新聞熱點
疑難解答
圖片精選