jquery ajax get return value

后端 未结 7 1533
不知归路
不知归路 2021-01-21 04:22

i want to get the \'printed value\' of html pages.

i tried below query, but showGetResult() just return \'null value\'

but my apache server logs printed i access

相关标签:
7条回答
  • 2021-01-21 04:38

    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 success handler:

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

    Also, don't use document.write.

    0 讨论(0)
  • 2021-01-21 04:42

    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:

    success:function(data) {
        document.write(data);
    }
    
    0 讨论(0)
  • 2021-01-21 04:45

    You have the wrong dataType per the documentation for jQuery.ajax:

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

    So you want to use html:

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


    In addition, as others have said, the ajax request is asynchronous. 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');
    
    0 讨论(0)
  • 2021-01-21 04:55

    This is the way AJAX works (asynchronously, like the name suggests). The showGetResult function returns before the AJAX call completes. showGetResult will therefore simply return null since that's what you've assigned to result.

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

    0 讨论(0)
  • 2021-01-21 04:59

    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>
    
    0 讨论(0)
  • 2021-01-21 05:00

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

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

    What you need to do is have document.write execute after success is invoked. 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>
    
    0 讨论(0)
提交回复
热议问题