简体   繁体   中英

AJAX data response - how to parse html

I have this response in my AJAX request:

    <!DOCTYPE html>
    <html>
    <head>
    <title>AJAX request</title>
    <style type="text/CSS">
    * {
    font-family:Courier New;
    }
    </style>
    </head>
    <body>
    1
    </body>
    </html>

but in

    $.ajax({
    ...
    complete:function(data){
    // data has only <title>AJAX request</title><style type="text/CSS">* {font-family:Courier New;} </style> 1
    }
    ...
    });

I can't parse it and how can I get 1 value from it? What I do wrong? I don't get all html tags, only title, style and content of body. How can I fix it?

There is no need to send full HTML via AJAX, but only the data needed to modify the existing DOM because the response gets dropped right after the callback functions are done.

So, if you only need that value (1 in your case), you can send it only, so the full response of your AJAX call would simply be:

1

and then your data variable would have the value of 1, of course.

You can also use JSON (especially, if you're sending more data), like:

{
  "value": 1,
  "somethingElse": "some value here"
}

etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM