AJAX POST请求在alert()中返回HTML代码

问题描述 投票:0回答:2

我有这个AJAX post请求:

$.ajax({
   type: 'post',
   url: 'gestioneFotografie.php', //The current page
   contentType: "application/json; charset=utf-8",
   data: {
      lat: lat,
      lng: lng
   },
   dataType: 'text',   
   success: function(data) {
      alert(data);
   }
});

问题是警报看起来像这样:enter image description here

jquery json ajax post alert
2个回答
0
投票

您无法通过alert()完成此操作。您可以使用所有Unicode字符和转义字符\n\t

或者您需要创建一个模式对话框,例如http://jqueryui.com/demos/dialog/https://getbootstrap.com/docs/4.5/components/modal/


0
投票

我从您的评论//The current page中了解到gestioneFotografie.php既是发布页面,也是响应页面。因此,gestioneFotografie.php主要是一个HTML界面,它另外处理用AJAX发布的数据。[您期望原始文本字符串作为响应,如dataType: 'text'所暗示,并且事实是,据我所知,您正在使用简单的alert()函数来显示响应。

警报框显示HTML的事实让我假设gestioneFotografie.php在以下一个或多个步骤中失败:

    捕获发布的数据
  1. 处理它们(在安全检查之后)
  2. 输出原始文本回复
  3. [exit,然后输出其他任何内容(包括HTML)
  4. 最后,您应确保gestioneFotografie.php具有类似于以下的全局结构:
  5. <?php if ( isset($_POST['lat'], $_POST['lng']) && mySanityCheck($_POST['lat'], $_POST['lng']) === 'safe' ) { $string = myDataProcessor($_POST['lat'], $_POST['lng']); exit($string); } // The rest of the code, including default HTML. ?>

© www.soinside.com 2019 - 2024. All rights reserved.