jQuery: Evaluate script in ajax response

前端 未结 4 1102
南笙
南笙 2020-11-30 15:13

XML responses from my webapp have both HTML to add to the page AND some have a script to run.

I\'m trying to send back XML from my webapp like:



        
4条回答
  •  感动是毒
    2020-11-30 15:56

    You can use the jQuery library to make the XML request to your backend and also parse it

    $(document).ready(function()
    {
      $.ajax({
        type: "GET",
        url: "your/url/that/returns/xml",
        dataType: "xml",
        success: function (xml) {
          // xml contains the returned xml from the backend
    
          $("body").append($(xml).find("html-to-insert").eq(0));
          eval($(xml).find("script").text());
        }
      });
    });
    

    You can find out more about jQuery here and here

    I haven't tested it, but it should work according to this article.

提交回复
热议问题