Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-11-2009, 07:40 AM   PM User | #1
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
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
rfresh is offline   Reply With Quote
Old 05-11-2009, 12:06 PM   PM User | #2
A1ien51
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
A1ien51 will become famous soon enough
And your mystery code would be?

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 05-11-2009, 04:04 PM   PM User | #3
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
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
rfresh is offline   Reply With Quote
Old 05-11-2009, 04:57 PM   PM User | #4
A1ien51
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
A1ien51 will become famous soon enough
create a new instance of the xmlHTTPRequest object and see if that fixes it.

Eric
__________________
Tech Author [Ajax In Action, JavaScript: Visual Blueprint]
A1ien51 is offline   Reply With Quote
Old 05-12-2009, 05:53 PM   PM User | #5
rfresh
Regular Coder

 
Join Date: Jun 2007
Location: Los Angeles
Posts: 545
Thanks: 81
Thanked 5 Times in 5 Posts
rfresh is an unknown quantity at this point
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
rfresh is offline   Reply With Quote
Old 05-12-2009, 07:15 PM   PM User | #6
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 is offline   Reply With Quote
Users who have thanked A1ien51 for this post:
rfresh (05-13-2009)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:08 PM.


Advertisement
Log in to turn off these ads.