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.
新闻热点
疑难解答
图片精选