Select content from JQuery ajax return object

前端 未结 3 955
自闭症患者
自闭症患者 2021-01-27 13:40

As I understand it, the JQuery load() function retrieves a document and can perform the quivelent of:

$(\'#somecontenttograb\').html()

upon it.

3条回答
  •  温柔的废话
    2021-01-27 14:13

    UPDATE

    In response to the comments you've left on other answers, I just want to make sure I understand the situation clearly:

    • You're testing from your localhost, not from the local hard drive (meaning your URL in the browser includes "http://localhost", and not a location on your hard drive.
    • When you make the AJAX response, alert(...)-ing the result give you null, or some equivalent.
    • The URL is valid and can be loaded in your browser.
    • The URL you're requesting is within the same domain as the originating page.

    If those things are true, then I'd try the following do some troubleshooting:

    1. Use Firefox and install Firebug (we'll need this to determine if AJAX requests are failing, and why)
    2. Include some logging in your code using Firebug's console
    3. Use jQuery's ajax(...) method, and handle failure by including some logging code to see what happened. You may be able to avoid this step if the page you're on doesn't need a visual response for a failed request and you're using Firebug.

    Here's the code I would use for this:

    $.ajax({
        url: "http://stackoverflow.com/",
        success: function(html) {
            var responseDoc = $(html);
            // Output the resulting jQuery object to the console
            console.log("Response HTML: ", responseDoc);
        }
        // Handle request failure here
        error: function(){
            console.log("Failure args: ", arguments);
        }
    });
    

    If you post the output of your Firebug logs, it should be easier to figure out the problem and find a solution for you. Firebug also logs XMLHttpRequests so you can see exactly what is being sent to and from the server, and Firebug will change the look of the request if it returns some kind of server error (which is how you could avoid #3 of the things I listed above).


    You could use the ajax(...) method, but it would be easier to use get(...) with a callback, like this:

    $.get("http://stackoverflow.com", function(html) {
        var responseDoc = $(html);
    });
    

    responseDoc will be a jQuery object you can use to extract elements from and treat just like you would any other jQuery object. You can extract stuff from the responseDoc and add it into your main document any way you want, using the jQuery manipulation methods.

    If you need the extra functionality the $.ajax(...) method provides, you would use the following code.

    $.ajax({
        url: "http://stackoverflow.com/",
        success: function(html) {
            var responseDoc = $(html);
        }
        error: function(XMLHttpRequest, textStatus, errorThrown){
            // Handle errors here
        }
    });
    

提交回复
热议问题