2012-01-14 23 views
1

我會試着把這個問題說得最好,因爲這很難解釋。基本上,我正在構建一個phonegap iphone應用程序,它連接到這個新聞站點並解析主要文章以供閱讀,主要使用jQuery函數load()。問題是,該網站有一個iffy移動版本,當使用iphone訪問時,有時會自動加載移動網站,有時會加載主網站。他們有不同的參考類別和ID(如我在移動網站周圍建立的),如果主站點被加載,我的應用程序完全沒用。該網站確實有一個腳本,設置(最有可能),確定是否加載移動或桌面網站會話變量,並在這裏找到:更改服務器上的移動網站?

http://www.macrumors.com/mr-toggleMobile.php?mobile=1 

A 1將設置移動網站和0將設置桌面網站。它適用於移動Safari和普通Safari瀏覽器。有沒有辦法,使用這個腳本和javascript/jquery,在使用load()方法時是否改變移動網站或桌面? (在少數情況下我需要桌面)。謝謝!

回答

1

而不是使用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

相關問題