Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>until now there is no solution for: abort ajax request using HttpClient object.</p> <p>I have been trying code to find out a solution but I gave up. Is not a big problem to detect when you have aborted a request and stop sending following requests in code, but the HttpClient object won't stop performing the requests that are already being sent.</p> <p><strong>If you can wait</strong> until next releases:</p> <p>Someone has filled in an issue regarding this problem <a href="https://jira.appcelerator.org/browse/TIMOB-15612" rel="nofollow">TIMOB-15612</a> and is already solved, maybe solves yours too.</p> <p><strong>If you can't</strong>:</p> <p>After some googling I saw <a href="http://developer.appcelerator.com/question/154693/httpclient---how-to-stop-an-upload-in-progress---abort" rel="nofollow">this post</a> where they point out that the solution comes by using <a href="http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Network.Socket.TCP" rel="nofollow">TCP sockets</a>, and they suggest looking some code <a href="http://developer.appcelerator.com/question/144392/android-httpclientabort-does-not-stop-file-upload" rel="nofollow">here</a></p> <p><strong>If what you want is</strong> to detect when you have aborted a request and stop sending following requests:</p> <p>According to <a href="http://developer.appcelerator.com/question/154693/httpclient---how-to-stop-an-upload-in-progress---abort" rel="nofollow">HTTPClient sdk 3.x documentation</a> the onload property:</p> <p>Must be set before calling open.</p> <p>And the same happens with ondatastream and onreadystatechange properties.</p> <p>Your index.js code should be reorganized and I'd suggest you something like this:</p> <pre><code>Ti.Network.HTTPClient.UNSENT = 0; Ti.Network.HTTPClient.OPENED = 1; Ti.Network.HTTPClient.HEADERS_RECEIVED = 2; Ti.Network.HTTPClient.LOADING = 3; Ti.Network.HTTPClient.DONE = 4; var ajaxCancel = false; var xhr = createHTTPClient(); var url = ""; //MUST BE SET BEFORE USE var params = ""; //MUST BE SET BEFORE USE function createHTTPClient(){ var xhr = Ti.Network.createHTTPClient(); xhr.onerror = function(e){ Ti.API.info("-----------------------"); var error = e.error; Ti.API.info("onerror:" + error); }; xhr.onreadystatechange= function(e){ Ti.API.info("-----------------------"); Ti.API.info("[onreadystatechange]readyState:" + xhr.readyState); }; xhr.onload= function(e){ Ti.API.info("-----------------------"); Ti.API.info("[onload]"); if (!ajaxCancel &amp;&amp; this.readyState === Ti.Network.HTTPClient.DONE) { Ti.API.info("[onload]success:");// + JSON.stringify(e)); }else{ Ti.API.info("[onload]readyState:" + xhr.readyState); if(ajaxCancel){ Ti.API.info("[onload]abortCall:");// + JSON.stringify(e)); }else{ Ti.API.info("[onload]:");// + JSON.stringify(e)); } } }; xhr.ondatastream = function(e){ Ti.API.info("-----------------------"); if(ajaxCancel){ Ti.API.info("[ondatastream]abortCall:");// + JSON.stringify(e)); }else{ Ti.API.info("[ondatastream]:");// + JSON.stringify(e)); } }; return xhr; } function sendAjax(){ if(ajaxCancel) return; Ti.API.info("----------CALL---------"); //xhr.open('GET', url); xhr.open('POST', url); Ti.API.info("Ti.Network.HTTPClient.UNSENT:" + Ti.Network.HTTPClient.UNSENT); Ti.API.info("Ti.Network.HTTPClient.OPENED:" + Ti.Network.HTTPClient.OPENED); Ti.API.info("Ti.Network.HTTPClient.HEADERS_RECEIVED:" + Ti.Network.HTTPClient.HEADERS_RECEIVED); Ti.API.info("Ti.Network.HTTPClient.LOADING:" + Ti.Network.HTTPClient.LOADING); Ti.API.info("Ti.Network.HTTPClient.DONE:" + Ti.Network.HTTPClient.DONE); Ti.API.info("[sendAjax]before request"); xhr.send(params); //params not needed for a GET operation Ti.API.info("[sendAjax]request send"); } function cancelAjax(){ if(!xhr || ajaxCancel) return; Ti.API.info("-----------------------"); Ti.API.info("[cancelAjax]abort petition"); xhr.abort(); Ti.API.info("[cancelAjax]abort call executed"); ajaxCancel = true; if(xhr.readyState === Ti.Network.HTTPClient.UNSENT || xhr.readyState === Ti.Network.HTTPClient.DONE){ //maybe it wasn't already sent or the cycle is completed Ti.API.info("[cancelAjax]request not send or already done: " + xhr.readyState); }else{ Ti.API.info("[cancelAjax]xhr.readyState:" + xhr.readyState); } } function allowAjaxCalls(){ ajaxCancel = false; } </code></pre> <p>In your index.xml add another label/button to control if you want to allow sending request after having canceled them:</p> <pre><code>&lt;Alloy&gt; &lt;Window backgroundColor="white"&gt; &lt;Button onClick="sendAjax"&gt;Send&lt;/Button&gt; &lt;Button onClick="cancelAjax"&gt;Cancel&lt;/Button&gt; &lt;Button onClick="allowAjaxCalls"&gt;AllowSend&lt;/Button&gt; &lt;/Window&gt; &lt;/Alloy&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload