Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    http.abort() Question

    When i use ajax http.abort() do I need to follow that up with another http. cmd? Once I abort one of my ajax functions, I can no longer execute any other ajax functions - when I click on another ajax link, nothing happens.

    Thanks
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    And your mystery code would be?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Here is the code - once I execute the abort function the search_function won't work anymore.

    PHP Code:
    function search_operation()
    {
        var 
    url "Ajax_Operation_ClickServer.php";
        var 
    params "search_text=" search_text "&replace_text=" replace_text;
        
    http.open("POST"urltrue);
        
    http.setRequestHeader("Content-type""application/x-www-form-urlencoded");
        
    http.setRequestHeader("Content-length"params.length);
        
    http.setRequestHeader("Connection""close");
        
    http.onreadystatechange = function()
             {
               if (
    http.readyState == 4//Finished loading the response
                  
    {
                    if (
    http.status == 200)
                       {
                         var 
    response http.responseText;
                         
    document.getElementById("id_search_results_container").innerHTML response;
                       }
                    else
                       {
                         
    alert("Ajax error: " http.statusText);
                       }
                  }
             }
        
    http.send(params);


    PHP Code:
    function abort_search_operation()
    {
        
    http.abort();
        return;

    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #4
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    create a new instance of the xmlHTTPRequest object and see if that fixes it.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    When I create this line

    var http = createRequestObject();

    the second time in the abort_operation(), will this work from a variable scoping point of view? I'm not too sure about how JS vars work with scoping their "visibility". I initially am creating the http instance at the top of my functions and then in abort_operation() I am trying to re-create it again - is this structure correct?

    Recall the problem is that when I abort an ajax operation (such as expand_search_window_section) and then try to call it again, it doesn't work. I have not tried this code below yet because I want to make sure the recreation of the http instance is in the correct spot from a var scoping point of view.

    The key question is where can/should I be recreating the var http = createRequestObject()?

    PHP Code:
    function createRequestObject() {
        if (
    window.XMLHttpRequest) { // Mozilla, Safari, Opera...
            
    var xmlhttp = new XMLHttpRequest();
            if (
    xmlhttp.overrideMimeType)
                
    xmlhttp.overrideMimeType('text/xml');
        }
        else if (
    window.ActiveXObject) { // IE
            
    try {
                var 
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (
    e) {
                try {
                    var 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (
    e) {}
            }
        }
        if (!
    xmlhttp) {
            
    alert('Giving up :( Cannot create an XMLHTTP instance');
            return 
    false;
        }
        return 
    xmlhttp;
    }

    var 
    http createRequestObject();

    function 
    abort_operation()
    {
        
    http.abort();
        var 
    http createRequestObject();
        return;
    }

    function 
    expand_search_window_section()
    {
        var 
    url "Ajax_ExpandSearch_ClickServer.php";
        var 
    params "php_status=" php_status;
        
    http.open("POST"urltrue);
        
    http.setRequestHeader("Content-type""application/x-www-form-urlencoded");
        
    http.setRequestHeader("Content-length"params.length);
        
    http.setRequestHeader("Connection""close");
        
    http.onreadystatechange = function()
             {
               if (
    http.readyState == 4//Finished loading the response
                  
    {
                    if (
    http.status == 200)
                       {
                         var 
    response http.responseText;
                         
    //alert(response);
                       
    }
                    else
                       {
                         
    alert("Ajax error: " http.statusText);
                       }
                  }
             }
        
    http.send(params);

    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    drop the var
    Code:
    function abort_operation()
    {
        http.abort();
        http = createRequestObject();
        return;
    }
    or

    Code:
        var params = "search_text=" + search_text + "&replace_text=" + replace_text;
        http = createRequestObject();
        http.open("POST", url, true);
    Your best bet would find a library to make the calls with proper OO JavaScript.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    rfresh (05-13-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •