2014-11-17 58 views
0

好了,我一直在努力得到這個現在幾個小時工作....AJAX onreadystatechange在post狀態更改之前執行?

這裏是我的簡單的Ajax請求:

功能ajaxRequest(){

var xmlhttp; 
var activexmodes = ["Msxm12.XMLHTTP", "Microsoft.XMLHTTP"]; 

if (window.XMLHttpRequest) 
{// code for IE7+, Firefox, Chrome, Opera, Safari 

return new XMLHttpRequest(); 

} 
else if (window.ActiveXObject) 
{// code for IE6, IE5 

    for(var i = 0; i < activexmodes.length; i++) 
    { 

     try 
     { 

      return new ActiveXObject(activexmodes[i]); 

     } 
     catch (e) 
     { 

      // Suppress 

     } 

    } 

} 
else 
{ 

    return false; 

} 

}

然後,我有這個函數調用它:

功能run_ajax_form_submit_request(s_method, s_processor,s_params,b_syn) {

var request = new ajaxRequest(); 
var message = null; 

    request.onreadystatechange=function() 
    { 

     if (request.readyState == 4) 
     { 

      if (request.status == 200 || window.location.href.indexOf("http") == -1) 
      { 

       if (request.responseText == 'true') 
       { 

        message = 'true'; 

       } 
       else 
       {  

        message = request.responseText; 

       } 

      } 
      else 
      { 

       messge = "There was a problem processing this request."; 

      } 

     } 
     else if (request.readyState == 0) 
     { 

      message = "Error: 0."; 

     } 
     else if (request.readyState == 1) 
     { 

      message = "Error: 1."; 

     } 
     else if (request.readyState == 2) 
     { 

      message = "Error: 2."; 

     } 
     else if (request.readyState == 3) 
     { 

      message = "Error: 3."; 

     } 

    } 

switch (s_method) 
{ 

    case 'POST': 

     request.open("POST", "./scripts/pages/index/index/form_processor.php?", true); 
     request.setRequestHeader("Content-s_input_type", "application/x-www-form-urlencoded"); 
     request.send(s_params); 

     break; 

    case 'GET': 

     request.open(s_method, s_processor, b_syn, true); 
     request.send(); 

     break; 

} 

return message; 

}

正在由該語句稱爲:

變種s_result = run_ajax_form_submit_request( 'POST',S_PATH,s_send_parameters,TRUE);

!!!結束代碼!!!

發生什麼事是代碼正確執行,直到它到達這一行: request.open(「POST」,「./scripts/pages/index/index/form_processor.php?」,true);

然後它運行onreadystatechange函數,由於沒有任何運行,它正在返回「錯誤1」。信息。

然後剩下的post腳本繼續並再次更改狀態,這次它運行4次,但onreadystatechange失敗再次運行,導致最終消息「Error 1。」...代碼將運行在調試器中正確運行,但無法在瀏覽器中運行...以下是JavaScript的第一頁,它調用了其他所有內容:

!!!初始代碼!!!

功能submit_post_form(s_form_name,S_PATH,b_restrict,s_success_response) {

var o_form = document.getElementById(s_form_name); 
var oa_inputs = o_form.getElementsByTagName('input'); 
var oa_selects = o_form.getElementsByTagName('select'); 
var o_submit = form_find_submit(oa_inputs); 
var o_messages = o_form.getElementsByClassName('messages')[0]; 

o_messages.className = 'messages'; 

switch (b_restrict) 
{ 

    case true: 

     for (i = 0; i < oa_inputs.length; i++) 
     { 

      oa_inputs[i].disabled = true; 

     } 

     for (i = 0; i < oa_selects.length; i++) 
     { 

      oa_selects[i].disabled = true; 

     } 

    break; 

} 

o_messages.classList.add('loading'); 

setTimeout(function(){ 

    o_messages.classList.add('active'); 

    setTimeout(function(){ 

     var sa_all_inputs_as_strings = new Array(); 

     var inputIDArray = new Array(); 
     var selectIDArray = new Array(); 

     var inputValArray = new Array(); 
     var selectValArray = new Array(); 

     var s_input_type = null; 

     for (i = 0; i < oa_inputs.length; i++) 
     { 

      s_input_type = oa_inputs[i].type; 
      var s_input_val_temp = null; 

      if (s_input_type.trim() != 'button' && s_input_type.trim() != 'submit') 
      { 

       sa_all_inputs_as_strings[i] = new Array(); 

       sa_all_inputs_as_strings[i][0] = oa_inputs[i].getAttribute('name'); 

       if (s_input_type == 'text' || s_input_type == 'password') 
       { 

        s_input_val_temp = oa_inputs[i].value; 

       } 
       else if (s_input_type == 'radio' || s_input_type == 'checkbox') 
       { 

        if (oa_inputs[i].checked) 
        { 

         s_input_val_temp = 'true'; 

        } 
        else 
        { 

         s_input_val_temp = 'false'; 

        } 

       } 

       sa_all_inputs_as_strings[i][1] = s_input_val_temp; 

      } 

     } 

     for (i = sa_all_inputs_as_strings.length; i < (sa_all_inputs_as_strings.length + oa_selects.length); ++i) 
     { 

      sa_all_inputs_as_strings[i] = new Array(); 

      sa_all_inputs_as_strings[i][0] = oa_selects.id; 
      sa_all_inputs_as_strings[i][1] = oa_selects.options[(oa_selects[selectInterval]).selectedIndex].value; 

     } 

     var s_send_parameters = "form=" + s_form_name; 

     for (i = 0; i < sa_all_inputs_as_strings.length; i++) 
     { 

      s_send_parameters += "&" + encodeURIComponent(sa_all_inputs_as_strings[i][0]) + "=" + encodeURIComponent(base64Encode(sa_all_inputs_as_strings[i][1])); 

     } 

     var s_result = run_ajax_form_submit_request('POST', s_path, s_send_parameters, true); 


     o_messages.classList.remove('active'); 

     setTimeout(function(){ 

      o_messages.className = 'messages'; 

      if (s_result == 'true') 
      { 

       o_messages.innerHTML = s_success_response; 
       o_messages.classList.add('success'); 

      } 
      else 
      {  

       o_messages.innerHTML = s_result; 
       o_messages.classList.add('error'); 


       for(i = 0; i < oa_inputs.length; i++) 
       { 

        if (oa_inputs[i].type != 'submit') 
        { 

         oa_inputs[i].disabled=false; 
         oa_inputs[i].classList.remove('disabled'); 

        } 

       } 

       for (i = 0; i< oa_selects.length; i++) 
       { 

        oa_selects[i].disabled=false; 
        oa_selects[i].classList.remove('disabled'); 

       } 

      } 

      setTimeout(function(){ 

       o_messages.classList.add('active'); 

      }, 100); 

     }, 200); 

    },200); 

},100); 

}

我不知道我做錯了......也許是超時(動畫),但我不知道....

回答

2

這就是它的工作方式,onreadystate事件在請求期間觸發多次,第一次在任何事情發送之前觸發,這就是爲什麼你必須聽數字4,你出於某種原因假設1,2等都是錯誤,它們不是,它們只是狀態碼的不同階段的請求。

的狀態碼如下

0:請求未初始化
1:服務器連接建立
2:請求接收
3:處理請求
4:請求完成和響應準備好

你會明白爲什麼1正在觸發,它不是一個錯誤碼,它是連接建立時的狀態碼。

您可以通過刪除所有添加的錯誤處理來解決此問題。

還有你的代碼的另一個問題,你設置了AJAX這種方式

function run_ajax_form_submit_request(s_method, s_processor, s_params, b_syn) { 
    var request = new ajaxRequest(); 
    var message = null; 

    request.onreadystatechange = function() { 
     if (request.readyState == 4) { 
      message = request.responseText; 
     } 
    } 

    return message; 
} 

和你調用它像

var s_result = run_ajax_form_submit_request('POST', s_path, s_send_parameters, true); 

但永遠不會因爲AJAX工作是異步,看到這個答案的詳細解釋

How to return the response from an Ajax call

+0

好吧,我拿出了所有的錯誤信息,但現在它根本沒有返回任何東西......調試器仍然正確,但瀏覽器仍然失敗...還有什麼錯誤? – Jingrah1

+0

是的,ajax調用是** async **,你不能這樣做。 – adeneo

+0

謝謝。我非常感激,我會在這裏花更長的時間來試圖解決這個問題。 – Jingrah1

相關問題