2014-12-28 5 views
1

Я пытаюсь создать центральную функцию для динамических веб-запросов.Как передать свойства объекта событию onSyndicationSuccess при использовании SMF.Net.WebClient динамически

function makeWebRequest(remoteURL, requestString, callBackFunction) { 
    var myWebRequest = new SMF.Net.WebClient({ 
     url : remoteURL, 
     httpMethod : "POST", 
     requestString : requestString, 
     requestHeaders : [ 
      "Content-Type: application/x-www-form-urlencoded"], 
     onSyndicationSuccess : callBackFunction, 
     onServerError : function (e) { 
      alert(e); 
     } 
    }); 
    myWebRequest.run(false); 
} 

При вызове makeWebRequest, передавая ему функцию callBackFunction;

var remoteURL = "http://parse.com/12/test"; 
var requestString = "category=news&type=world"; 

function callBackFunction(e) { 
    responseText = this.responseText; 

    if (responseText != null) { 
     parsedJSON = JSON.parse(responseText); 
    } 
} 

makeWebRequest(remoteURL,requestString,callBackFunction); 

Применение выдает ошибку в строке responseText = this.responseText;

Как я могу передать myWebRequest себя функции, как это?

ответ

1

Я использовал ваш код. Я просто добавляю textButton к странице, и он отлично работает как для Android, так и для iOS.

В Global.js;

function makeWebRequest(remoteURL, requestString, callBackFunction) { 
    var myWebRequest = new SMF.Net.WebClient({ 
     url : remoteURL, 
     httpMethod : "POST", 
     requestString : requestString, 
     requestHeaders : [ 
      "Content-Type: application/x-www-form-urlencoded"], 
     onSyndicationSuccess : callBackFunction, 
     onServerError : function (e) { 
      alert(e); 
     } 
    }); 
    myWebRequest.run(false); 
} 


var remoteURL = "http://parse.com/12/test"; 
var requestString = "category=news&type=world"; 

function callBackFunction(e) { 
    var responseText = this.responseText; 
    alert(responseText); 

    if (responseText != null) { 
     parsedJSON = JSON.parse(responseText); 
    } 
} 



function Global_Events_OnStart(e) { 
    changeLang(Device.language, true); 
    include("BC.js"); //included for future BC support. Removing is not advised. 

    //  Comment following block for navigationbar/actionbar sample. Read the JS code file for usage. 
    //  Also there is a part of code block in Page1, which should be copied to every page for HeaderBar usage 
    load("HeaderBar.js"); 
    header = new HeaderBar(); 

    //  Uncomment following block for menu sample. Read the JS code file for usage. 
    /* 
    load("Menu.js"); 
    /**/ 

} 
function Global_Events_OnError(e) { 
    switch (e.type) { 
    case "Server Error": 
    case "Size Overflow": 
     alert(lang.networkError); 
     break; 
    default: 
     SES.Analytics.eventLog("error", JSON.stringify(e)); 
     //change the following code for desired generic error messsage 
     alert({ 
      title : lang.applicationError, 
      message : e.message + "\n\n*" + e.sourceURL + "\n*" + e.line + "\n*" + e.stack 
     }); 
     break; 
    } 
} 

In Page1.js;

function Page1_Self_OnKeyPress(e) { 
    if (e.keyCode === 4) { 
     Application.exit(); 
    } 
} 
function Page1_Self_OnShow() { 
    //Comment following block for removing navigationbar/actionbar sample 
    //Copy this code block to every page onShow 
    header.init(this); 
    header.setTitle("Page1"); 
    header.setRightItem("RItem"); 
    header.setLeftItem(); 
    /**/ 
} 
function Page1_TextButton1_OnPressed(e){ 
    makeWebRequest(remoteURL,requestString,callBackFunction); 
} 
+1

Вы правы. Как-то он работает, и я не знаю, почему ... Спасибо в любом случае ... – atakan

1

все нормально работает. Проверьте свою функцию makeWebRequest, она должна быть на Global.js. Также определите переменную responseText с помощью «var».

+0

он уже находится в Global.js, и оба responseText и parsedJSON определены глобально. callBackFunction не может получить свойства веб-клиента – atakan