而不是使用load()
,請使用ajax()
。
這樣你可以設置url的data
選項。這裏是data
的解釋:
"Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below)."
對於如:
$.ajax({
url:"http://www.macrumors.com/mr-toggleMobile.php",
data:"mobile=1",
error: function(jqXHR, textStatus, errorThrown){
//textStatus is the error text, like timeout or abort
},
success:function(data){
//data is the downloaded page. Make your stuff here
}
});
既然你說,有時你需要一個或其他,你可以做一個函數:
function getData(var){
$.ajax({
url:"http://www.macrumors.com/mr-toggleMobile.php",
data:"mobile="+var,
error: function(jqXHR, textStatus, errorThrown){
//textStatus is the error text, like timeout or abourt
return "Error: "+textStatus;
},
success:function(data){
//data is the downloaded page. Make your treatment here
return data;
}
});
}
然後你可以這樣稱呼:
var newsPage = getData(1); //1 for mobile, 0 for normal
//do whatever you need with newsPage, like a if for checking if came starting with 'Error'. If not, then seems ajax was successful.
有關更多說明和選項,請檢查$.ajax() at jQuery's API Page