Retrieving only part of a file

I have a very simple code example:

$.ajax({
  url: 'demo2.htm',
  success: function(loadeddata){
    $("#loaded_data").html(loadeddata);
    alert('success');
  },
  error: function(){
    alert('failure');
  }
});

Downloaded data currently returns everything. I need to get only a specific div and make it html #loaded_data.

How to do it?

Thanks!

Edit:

When trying to use .load () ...

Here is what I wrote in the comment.

Thank you, your updated example is wonderful. However, I am not sure with my mistake.

It works

$("#loaded_data").load("demo2.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

It:

$("#loaded_data").load("demo5.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

No. He is just hanging. No demo5.htm But the error is not returned.

Thank you very much for your help.

+3
source share
2 answers

You can use .load()for this:

$("#loaded_data").load("demo2.htm #mydiv", function(text, status, request) {
    if(status == 'success') {
        alert('success');
    } else {
        alert('error');
    }
});

If this does not work for you, you are doing something wrong, and we will need to see some more code.

div , .

+5

, , , , ( )... , , ...

textStatus .

$("#links").load("/Main_Page #jq-p-Getting-Started li", 
  function (responseText, textStatus, XMLHttpRequest) {
    if (textStatus == "success") {
         // all good!
    }
    if (textStatus == "error") {
         // oh noes!
    }
  }

:

:

http://jsbin.com/ageju

:

http://jsbin.com/arite

+1

Source: https://habr.com/ru/post/1709560/


All Articles