jquery ajax get responsetext from http url

后端 未结 9 1304
盖世英雄少女心
盖世英雄少女心 2020-12-07 19:14

Neither:

var response = $.ajax({
    type: \"GET\",   
    url: \"http://www.google.de\",   
    async: false,
    success : function() {
        alert (this         


        
相关标签:
9条回答
  • 2020-12-07 19:53

    try this

    alert( data['responseText'] );
    
    0 讨论(0)
  • 2020-12-07 20:01

    First you have to download a JQuery plugin to allow Cross-domain requests. Download it here: https://github.com/padolsey/jQuery-Plugins/downloads

    Import the file called query.xdomainsajax.js into your project and include it with this code:

    <script type="text/javascript" src="/path/to/the/file/jquery.xdomainajax.js"></script>
    

    To get the html of an external web page in text form you can write this:

    $.ajax({
        url: "http://www.website.com",
        type: 'GET',
        success: function(res) {
            var text = res.responseText;
            // then you can manipulate your text as you wish
        }
    });
    
    0 讨论(0)
  • 2020-12-07 20:02

    in jquery ajax functions, the success callback signature is:

    function (data, textStatus) {
      // data could be xmlDoc, jsonObj, html, text, etc...
      this; // the options for this ajax request
    }
    

    depending on the data type you've asked, using the 'dataType' parameter, you'll get the 'data' argument.

    from the docs:

    dataType (String) Default: Intelligent Guess (xml or html). The type of data that you're expecting back from the server. If none is specified, jQuery will intelligently pass either responseXML or responseText to your success callback, based on the MIME type of the response.

    The available types (and the result passed as the first argument to your success callback) are:

    "xml": Returns a XML document that can be processed via jQuery.

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

    "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching unless option "cache" is used. Note: This will turn POSTs into GETs for remote-domain requests.

    "json": Evaluates the response as JSON and returns a JavaScript Object.

    "jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback. (Added in jQuery 1.2)

    "text": A plain text string.

    see http://docs.jquery.com/Ajax/jQuery.ajax#options

    0 讨论(0)
提交回复
热议问题