简体   繁体   English

jquery ajax 获取返回值

[英]jquery ajax get return value

i want to get the 'printed value' of html pages.我想获得 html 页面的“打印值”。

i tried below query, but showGetResult() just return 'null value'我试过下面的查询,但 showGetResult() 只是返回“空值”

but my apache server logs printed i accessed index.php when i try this code.但是当我尝试这段代码时,我的 apache 服务器日志打印了我访问了 index.php。

(index.php just print helloworld) (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>

This is the way AJAX works (asynchronously, like the name suggests).这就是 AJAX 的工作方式(异步,顾名思义)。 The showGetResult function returns before the AJAX call completes. showGetResult函数在 AJAX 调用完成之前返回。 showGetResult will therefore simply return null since that's what you've assigned to result .因此, showGetResult将简单地返回null因为这是您分配给result

Move any code that depends on the result of the AJAX call inside the success callback.success回调中移动依赖于 AJAX 调用结果的任何代码。 Alternatively, you could make the call synchronous, but that's not usually what you want.或者,您可以使调用同步,但这通常不是您想要的。

I think what you want to do is this.我想你想做的是这个。

<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>

AJAX requests are aynchronous by default; AJAX 请求默认是异步的; you can't return their result in a function, you need to use callbacks.你不能在函数中返回他们的结果,你需要使用回调。 The easiest way to achieve what you want is to put your code that handles your data in your success handler:实现您想要的最简单方法是将处理数据的代码放在success处理程序中:

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

Also, don't use document.write .另外,不要使用document.write

You have the wrong dataType per the documentation for jQuery.ajax :根据jQuery.ajax文档,您有错误的dataType

"html": Returns HTML as plain text; "html": 以纯文本形式返回 HTML; included script tags are evaluated when inserted in the DOM.包含的脚本标签在插入 DOM 时进行评估。

So you want to use html :所以你想使用html

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


In addition, as others have said, the ajax request is asynchronous. 此外,正如其他人所说,ajax 请求是异步的。 So you need to restructure your code. 所以你需要重构你的代码。 For example: 例如:

 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');

You're missing a fundamental point here.你在这里错过了一个基本点。 The success method is not run when you call showGetResult.调用 showGetResult 时不会运行success方法。 It is run asynchronously .它是异步运行的。

At the point that you put return result;在您放置return result; it is still null (because success has not yet been invoked).它仍然为空(因为尚未调用success )。

What you need to do is have document.write execute after success is invoked.您需要做的是在调用成功后执行 document.write。 Either like this:要么像这样:

<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>

Or with a callback:或者使用回调:

<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>

Rather than using document.write on what you expect the function to return, the success callback can take care of that for you, like so:成功回调可以为您处理这些,而不是在您期望函数返回的document.write上使用document.write ,如下所示:

success:function(data) {
    document.write(data);
}
jQuery.ajax({
async: false, //add async false
        url: 'http://localhost/index.php',
        type: 'get',
        dataType: 'text/html',
        success:function(data)
        {
            alert(data);
            result = data;
        } 
     });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM