2011-03-14 48 views
0

php腳本正在返回一個值,第1條警告起作用。Javascript Returing來自httprequest的價值

我無法引用httprequest在第二次提醒時返回的值。理想情況下,我會調用函數get_captcha() - 它會返回值 - 它只是我不知道如何做到這一點。

我意識到在全球範圍內設置變量可能不是最好的方法來做到這一點,但它是我能想到的唯一的事情 - 我打開替代品。

<script type="text/javascript"> 
    var url = "captcha_get_code.php"; // The server-side script 
    var cap; 

    function ValidateForm() { 
    get_captcha() 
    alert(cap); //undefined 

    } 

    function get_captcha() {  
     http.open("GET", url, true); 
     http.onreadystatechange = handleHttpResponse; 
     http.send(null); 

} 

    function handleHttpResponse() {  
    if (http.readyState == 4) { 
     if (http.status==200) { 
      //return http.responseText; 
     cap=http.responseText; 
      alert(cap); //this one works 
      } 

     } 
    } 



function getHTTPObject() { 
    var xmlhttp; 

    if(window.XMLHttpRequest){ 
    xmlhttp = new XMLHttpRequest(); 
    } 
    else if (window.ActiveXObject){ 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    if (!xmlhttp){ 
     xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 

} 
    return xmlhttp; 
} 
var http = getHTTPObject(); // We create the HTTP Object 





</script> 
+0

我建議你爲這些http調用使用jQuery - 它會爲你節省很多的努力。 – pimvdb 2011-03-14 18:53:03

回答

6

不能從成功的XMLHttpRequest調用 「迴歸」 的價值觀。你可以執行任何你需要的處理裏面的的回調函數。

XMLHttpRequests是異步執行的。你不能讓你的代碼「等待」他們(除非你讓他們同步)(你真的,真的不應該這樣做)。但是,並不需要真正的需求,因爲運行時系統會在請求完成時調用您的「readystatechange」處理程序。從那個代碼中,你可以自由地做你需要的任何事情。

這個事實要求你對如何編寫代碼有一些不同的想法,但這並不是一個很大的調整。例如,如果您傾向於編寫「processResults()」函數,那麼您仍然可以執行—,您只需從內部調用「readystatechange」處理程序。

1

我看到這個線程是4歲,但它有錯誤的答案

您可以從成功的XMLHttpRequest調用中獲取返回值。 我的項目使用WebSocket,但它使用XMLHttpRequest上傳圖像。

 
    In a javascript, call uploadSend(containerID) where all <img> are stored. 


    // ----- uploadSend() 
    // ----- This function sends all objects in a container (containerID) 
    // ----- All object has to be <img> 

FILE: upload.js 

    function uploadSend(containerID) { 
     var elm = document.getElementById(containerID); 
     for (i=0; i<elm.childNodes.length; i++) { 
     var form = new FormData(); 
     form.append('id', elm.childNodes[i].id); 
     form.append('src', elm.childNodes[i].src); 
     TOS(form); 
     } 
    } 

    function xhrState(self) { 
     if ((self.readyState == 4) && (self.status === 200)) 
     console.log(self.responseText); 
    } 

    function xhrProgress(event) { 
     if (event.lengthComputable) 
     if (event.loaded == event.total) 
      console.log('Image 100% uploaded.'); 
    } 

    function TOS(form) { 
     var xhr = new XMLHttpRequest(); 
     xhr.onreadystatechange = function() { xhrState(this); } 
     xhr.open('POST', '/upload.php'); 
     xhr.upload.onprogress = function(event) { xhrProgress(event); } 
     xhr.send(form); 
    } 

FILE: upload.php 

    header("Content-type: text/plain"); 

    $filename = '/var/www/public/upload/'.microtime(true); 

    // ----- Save a complete file for what we did get. 
    $hnd = fopen($filename . 'TXT', 'wb'); 
    fwrite($hnd, print_r($_COOKIE, true)); 
    fwrite($hnd, print_r($_GET, true)); 
    fwrite($hnd, print_r($_FILES, true)); 
    fwrite($hnd, print_r($_POST, true)); 
    fclose($hnd); 

    // ----- Save just jpg for the images we did get. 
    $hnd = fopen($filename . 'jpg', 'wb'); 
    $image = explode(',', $_POST['src']); 
    fwrite($hnd, base64_decode($image[1])); 
    fclose($hnd); 

    // ----- Type the result that you want back. 
    echo "{$filename}.jpg";