Javascript jquery ajax 获取返回值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10194684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:16:05  来源:igfitidea点击:

jquery ajax get return value

javascriptjqueryajax

提问by osmund sadler

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>

采纳答案by Isaac Fife

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>

回答by James Allardice

This is the way AJAX works (asynchronously, like the name suggests). The showGetResultfunction returns before the AJAX call completes. showGetResultwill therefore simply return nullsince that's what you've assigned to result.

这就是 AJAX 的工作方式(异步,顾名思义)。该showGetResult函数在 AJAX 调用完成之前返回。showGetResult因此将简单地返回,null因为那是您分配给的内容result

Move any code that depends on the result of the AJAX call inside the successcallback. Alternatively, you could make the call synchronous, but that's not usually what you want.

success回调中移动依赖于 AJAX 调用结果的任何代码。或者,您可以使调用同步,但这通常不是您想要的。

回答by Kato

You're missing a fundamental point here. The successmethod is not run when you call showGetResult. It is run asynchronously.

你在这里错过了一个基本点。success调用 showGetResult 时不会运行该方法。它是异步运行的。

At the point that you put return result;it is still null (because successhas not yet been invoked).

在你放置return result;它的时候它仍然是空的(因为success还没有被调用)。

What you need to do is have document.write execute after success is invoked. Either like this:

您需要做的是在调用成功后执行 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>

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>

回答by cchana

Rather than using document.writeon what you expect the function to return, the success callback can take care of that for you, like so:

document.write成功回调可以为您处理这些,而不是使用您期望函数返回的内容,如下所示:

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

回答by Moaz Salem

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

回答by Alex Turpin

AJAX requests are aynchronous by default; 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 successhandler:

AJAX 请求默认是异步的;你不能在函数中返回他们的结果,你需要使用回调。实现您想要的最简单方法是将处理数据的代码放在处理success程序中:

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

Also, don't use document.write.

另外,不要使用document.write.

回答by Justin Ethier

You have the wrong dataTypeper the documentation for jQuery.ajax:

dataType根据jQuery.ajax文档,您有错误:

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

"html": 以纯文本形式返回 HTML;包含的脚本标签在插入 DOM 时进行评估。

So you want to use html:

所以你想使用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');