How to display HTML code without interpreting the latter by the browser as instructions?
Code:

<script> $.ajax({ url: "https://api.vk.com/method/users.get", data: { user_ids: '1', v: "5.26" }, dataType: "jsonp", success: function (e) {alert(e.response[0].first_name + ' ' + e.response[0].last_name);} }); </script> 

That is, I have an alert displayed, the first name was in my div block on the page itself.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • instead of an alert, write document.getElementByID('ваш_див').innerHTML = e.response[0].first_name and so on. - Jean-Claude

1 answer 1

Turn specials. characters in HTML mnemonics :

 &#x3C;script&#x3E; $.ajax({ url: &#x22;https://api.vk.com/method/users.get&#x22;, data: { user_ids: &#x27;1&#x27;, v: &#x22;5.26&#x22; }, dataType: &#x22;jsonp&#x22;, success: function (e) {alert(e.response[0].first_name + &#x27; &#x27; + e.response[0].last_name);} }); &#x3C;/script&#x3E; 

UPD :

If you need to execute the code and display the browser only the request response, then:

 // Если в ответе будут спец. символы, эта функция их превратит в мнемоники function encodeHTML(raw){ return raw.replace(/[\u00A0-\u9999<>\&]/gim, i => '&#' + i.charCodeAt(0) + ';'); } $.ajax({ url: "https://api.vk.com/method/users.get", data: { user_ids: '1', v: "5.26" }, dataType: "jsonp", success: function(e) { // Ищем нужный div (тут это #response) и вставляем туда ответ, пропущенный через мясорубку спец. символов // (чтобы не нароком не выполнился) document.querySelector('#response').innerHTML = encodeHTML(e.response[0].first_name + ' ' + e.response[0].last_name); //alert(e.response[0].first_name + ' ' + e.response[0].last_name); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='response'>Answer</div> 

  • You misunderstand, you need the script to work, but the result is to display not in the alert window, but in html - user209600
  • @DMEE, in the question you indicated exactly the output of this code as text, everyone agreed, the revision accepted. Write the task correctly. - user207618