2011-06-09 26 views
0
var JsonClientPatientSearch = Titanium.Network.createHTTPClient(); 

    // API Url to call 
    var url = GetAPIUrl() + "PatientSearch"; 
    JsonClientPatientSearch.open("POST", url); 
    //setting Request Header 
    JsonClientPatientSearch.setRequestHeader("Content-type", "application/json"); 
     JsonClientPatientSearch.send(PatientSearch(PatientSearchCriteria,Credentials,Header));  

    JsonClientPatientSearch.onload = function(){ 

    }; 
    JsonClientPatientSearch.onerror = function(e){ 

    }; 

我有很多JSON在我的項目要求,是有可能,我可以寫一個類,並使用它的實例,使JSON呼叫...只是傳遞參數...常見JSON Call類

+1

JavaScript中的「class」是不可能的。但是你可以做一些解決方法來創建可能對你有幫助的「對象」。看看:http://www.phpied.com/3-ways-to-define-a-javascript-class/ – Nobita 2011-06-09 07:50:58

回答

2

您可以創建對象的實例並重用它們。你的代碼看起來像這樣:

var MyCall = function(url, onLoad, onError){ 
    // API Url to call 
    this.url = GetAPIUrl() + url; 
    this.onLoad = onLoad; 
    this.onError = onError; 
}; 
MyCall.prototype = { 
    call: function(){ 
     var JsonClientPatientSearch = Titanium.Network.createHTTPClient(); 
     JsonClientPatientSearch.open("POST", this.url); 
     //setting Request Header 
     JsonClientPatientSearch.setRequestHeader("Content-type", "application/json"); 
     JsonClientPatientSearch.send(PatientSearch(PatientSearchCriteria,Credentials,Header));  
     JsonClientPatientSearch.onload = this.onLoad; 
     JsonClientPatientSearch.onerror = this.onError; 

    } 
}; 

// create callbacks 
var myLoad = function(response){ /* do something with response */ }, 
    myError = function(error){ /* do something with error */ }; 
// create instance 
new MyCall("PatientSearch", myLoad, myError); 
// do a call 
MyCall.call(); 

你需要根據這個需要如何與你的其他全局對象一起調整。但希望這會讓你朝正確的方向發展。

+0

我應該在哪裏寫onLoad和onError函數,我將如何收到responseText – 2011-06-09 08:59:18

+0

對不起,如果忘記了以包括回調...如果可以,我編輯了示例 – 2011-06-09 10:25:27

+0

,但我可能會建議爲ajax調用使用庫。 – 2011-06-09 10:26:07