Jquery ajax get return value

I want to get the "printed value" of html pages.

I tried to execute the request below, but showGetResult () would just return 'null value'

but my Apache server logs printed I access index.php when I try this code.

(index.php just prints 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
source share
6 answers

I think you want to do 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>
+3
source

This is how AJAX works (asynchronously, like the name). The function showGetResultreturns until the AJAX call ends. Therefore, it showGetResultsimply returns nullfrom the time you appointed result.

, AJAX success. , , .

+5

AJAX ; , . , , - , success:

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

, document.write.

0

dataType jQuery.ajax:

"html": HTML ; script 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

You are missing a fundamental point here. The method successdoes not start when showGetResult is called. It runs asynchronously .

At the moment you put it return result;, it is still zero (since it successhas not yet been called).

What you need to do is execute document.write after starting. Or 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>
0
source

Instead of using document.writewhat you expect to return functions, a success callback can take care of this for you:

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

All Articles