【问题标题】:Does not display the message using JSON AJAX php online在线使用 JSON AJAX php 不显示消息
【发布时间】:2017-10-16 01:06:30
【问题描述】:

我对这段代码有疑问, 这个想法是按下 btn_verify 按钮,它向我显示一条消息“这是 JSON”,奇怪的是程序在 localhost 中运行, 但是当上传到主机时不运行并且不显示消息 是什么原因? 我有其他程序使用相同的结构和库并且运行良好 两种形式:本地和托管。

我的代码是:

//HTML

<!DOCTYPE html>
<html>
<head>
   <title>Resultados</title>

<style type="text/css">
 td{font-family: Arial; font-size: 9pt;}
</style>

<style type="text/css">
  th{font-family: Arial; font-size: 9pt;}
</style>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="public/css/bootstrap.min.css" />
</head>
<body>

<button type="button" name="btn_verificar" id="btn_verificar"  class="btn 
btn-info">Verificar</button>

<div class="the-return">

</div>

<script src="public/js/jquery-3.1.1.js"></script>                           
<script src="public/js/bootstrap.min.js"></script>
<script type="text/javascript" src="script/functions.js"></script>

   <script>
    $(document).on('click','#btn_verificar',function(){
      $(document).ready(function(){
       $('#btn_verificar').click(function(){  

       var id=$('#id_pedido').val();//take id about one record and it is ok

       //alert(id); show id and this out it's right

      $.ajax({
         method:"POST",
         url:"verificarResultados.php",        
         data:{id:id},
         dataType:"JSON",        
         success:function(data)
          {            
             alert(data.result);

             $(".the-return").html("Message:" + data["result"]);

          }//end  success        
      }); //end  ajax   
    });// end document ready
  });//end on click

 </script>
 </body>
</html>



 //file verificarResultados.php

 <?php

  if(isset($_POST["id"]))
   {    
     $rows = array("result" => 'This is JSON');
     echo json_encode($rows);             
   }   
 ?>

【问题讨论】:

  • 您是否尝试在浏览器的 Web 控制台中查看?
  • 警报有效吗?你可以控制台记录数据吗?您可以查看控制台中的网络选项卡并查看发送的内容和接收的内容吗?为什么在 .click 函数中有 document.ready ???您在控制台中是否收到任何 javascript 错误???
  • #id_pedido 元素在哪里?
  • 您使用 Web 控制台找到什么了吗?此外,如果您在托管中使用确切的代码,它将无法正常工作,因为正如@JorgeGuberte 所说,缺少 ID 为 id_pedido 的元素。也许这就是导致问题的原因

标签: php jquery json ajax


【解决方案1】:
$.ajax({
       method:"POST",
       url:"verificarResultados.php",        
       data:{id:id},
       dataType:"JSON",
        success: function (data) {
            alert(data.result);
        },
        error: function (xhr, result, error) {
            alert(xhr.responseText);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 2016-12-27
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多