jquery ajax get return value

后端 未结 7 1547
不知归路
不知归路 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: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');
    

提交回复
热议问题