2012-04-17 120 views
3

我想獲得html頁面的'打印值'。jquery ajax獲取返回值

我想下面的查詢,但showGetResult()只是返回「空值」

但我的Apache服務器日誌打印我訪問的index.php當我嘗試此代碼。

(的index.php只是打印的HelloWorld)

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script> 
<script type="text/javascript"> 
function showGetResult(name) 
{ 
    var result = null; 
    jQuery.ajax({ 
     url: 'http://localhost/index.php', 
     type: 'get', 
     dataType: 'text/html', 
     success:function(data) 
     { 
      alert(data); 
      result = data; 
     } 
    }); 
    return result; 
} 

document.write(showGetResult('test')); 
</script> 

回答

3

我想你要做的是這樣的。

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script> 
<script type="text/javascript"> 
function showGetResult(name) 
{ 
    jQuery.ajax({ 
     url: 'http://localhost/index.php', 
     type: 'get', 
     dataType: 'text/html', 
     success:function(data) 
     { 
      alert(data); 
      document.write(data); 
     } 
    }); 
} 

showGetResult('test'); 
</script> 
0

AJAX請求默認情況下aynchronous;你不能在函數中返回他們的結果,你需要使用回調函數。達到你想要什麼,最簡單的方法就是把你的代碼來處理你的success處理你的數據:

success:function(data) 
    { 
     alert(data); 
     result = data; 
     document.write(showGetResult('test')); 
    } 

另外,不要使用document.write

5

這是AJAX的工作方式(異步,如名稱所示)。在AJAX調用完成之前,showGetResult函數會返回。因此,showGetResult只會返回null,因爲這是您分配給result的內容。

移動任何取決於success回調內的AJAX調用結果的代碼。或者,您可以使呼叫同步,但這通常不是您想要的。

0

你必須每個文檔的錯誤dataTypejQuery.ajax

「HTML」:返回HTML純文本;包含的腳本標記在插入到DOM中時被評估。

所以要使用html

... 
dataType: 'html', 
... 


此外,正如其他人所說,Ajax請求是異步的。所以你需要重構你的代碼。例如:

function showGetResult(name) 
{ 
var result = null; 
jQuery.ajax({ 
    url: 'http://localhost/index.php', 
    type: 'get', 
    dataType: 'html', 
    success:function(data) 
    { 
     alert(data); 
     result = data; 
     document.write(result); 
    } 
}); 
} 

showGetResult('test'); 
0

您錯過了一個基本點。調用showGetResult時,不運行success方法。它運行asynchronously

在您放入return result;時它仍然爲空(因爲success尚未調用)。

你需要做的是讓document.write在調用成功後執行。無論是這樣的:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script> 
<script type="text/javascript"> 
function showGetResult(name) 
{ 
    var result = null; 
    jQuery.ajax({ 
     url: 'http://localhost/index.php', 
     type: 'get', 
     dataType: 'text/html', 
     success:function(data) 
     { 
      alert(data); 
      document.write(data); 
     } 
    }); 
    return result; 
} 

//document.write(showGetResult('test')); 
showGetResult('test'); 
</script> 

還是有回調:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"><\script> 
<script type="text/javascript"> 
function showGetResult(name) 
{ 
    var result = null; 
    jQuery.ajax({ 
     url: 'http://localhost/index.php', 
     type: 'get', 
     dataType: 'text/html', 
     success:function(data) 
     { 
      alert(data); 
      writeToDocument(data); 
     } 
    }); 
} 

function writeToDocument(data) { 
    document.write(data); 
} 

showGetResult('test'); 
</script> 
0

而不是你所期望的返回功能使用document.write,成功回調可以照顧,對你,就像這樣:

success:function(data) { 
    document.write(data); 
}