duan0514324 2018-06-01 20:35
浏览 65
已采纳

Ajax请求,但没有得到PHP脚本的答案

I am working with a jQuery-function to trigger an ajax request on click. However, I don't get a result though the script should send a value via post-method. It looks like the PHP-script doesn't receive the value, but probably the problem is elsewhere.

Maybe somebody can tell me where the problem is.

doClick = function (sender){
var jid = sender.id;
if ($(window).width() < 768) {
       $('.jwin').addClass('order-md-1').removeClass('order-md-2').removeClass('d-none');
       $('.jobs').addClass('order-md-2').removeClass('order-md-1').removeClass('d-block').addClass('d-none');
          var ink = document.getElementById('ajax').value;

          $.ajax({
           url:"get_job.php",
           method:"POST",
           data:{jid:jid},
           cache:false,
           dataType: "json",
           contentType: "application/json; charset=utf-8",
           success:function(data)
           {
                //console.log(data.success);
                if(data.success == TRUE)
                   {
                      //$('#ajax').text(data.nurl);
                      $('#title').text(data.title);
                      $('#crp').text(data.compnme);
                      $('#descript').html(data.jtxt);
                      $('#jadoci').text(data.city);     
                      $('#jadost').text(data.state);                          
                      $('#connx1').css("display", "inline");                                                                  
                      $('#connx2').css("display", "inline");
                      $('#applylink').css("display","block");
                      $('#applylink').attr('href',"links?jid="+jid);    
                      if(data.url==''){
                        $('#applylink').css("display","none");
                      }

                   }else{
                      // Fade in
                      alert(data.success);
                   }
           }
          });

       $('#returnmobile').append("<a href=''>"+jid+"</a>");
    }
else {

          $.ajax({
           url:"get_job.php",
           method:"POST",
           data:{jid:jid},
           cache:false,
           dataType: "json",
           contentType: "application/json; charset=utf-8",         
           success:function(data)
           {
                //console.log(data.success);
                if(data.success == TRUE)
                   {
                      //$('#ajax').text(data.nurl);
                      $('#title').text(data.title);

                      $('#descript').html(data.jtxt);

                      if(data.company==0){
                        $('#crp').text("Agent");                            
                      }
                      else{
                          $('#crp').text(data.compnme);                       
                          $('#crp').attr('href','company?id='+data.company)                     
                      }

                      $('#jadoci').text(data.city);     
                      $('#jadost').text(data.state);
                      $('#connx1').css("display", "inline");                                                                  
                      $('#connx2').css("display", "inline");    
                      $('#applylink').css("display","block");
                      $('#applylink').attr('href',"links?jid="+jid);                            
                      if(data.url==''){
                        $('#applylink').css("display","none");
                      }
                   }else{
                      // Fade in
                      alert(data.success);
                   }
           }
          });      

       //alert('More than 960');
}
};    

And the PHP-code:

require "config.php";


$jid = $_POST["jid"];


$ipa = $_SERVER['REMOTE_ADDR'];



    $query = $con->query("SELECT jla.title AS title, jla.summary AS jtxt, jla.city AS city, jla.state AS state, jla.company AS corp, c.name AS company, jla.url AS url, jla.id AS coid FROM jobs jla LEFT JOIN companies c ON c.id = jla.company WHERE jla.id = $jid");

    if($query->num_rows > 0){ 
        while($row = $query->fetch_assoc()){

          $output =  array('success'=>TRUE,
                           'nurl'=>("<b>Does</b> it work? Yes!"),
                           'company'=>$row['corp'],
                           'compnme'=>$row['company'],
                           'title'=>$row['title'],
                           'city'=>$row['city'],
                           'state'=>$row['state'],                                                      
                           'coid'=>$row['coid'],
                           'url'=>$row['url'],                           
                           'jtxt'=>$row['jtxt']);      
        $quip0 = $con->query("SELECT * FROM clicks WHERE ip = '$ipa' AND job_id = '$jid'");
        if($quip0->num_rows < 1){
          $quip1 = "INSERT INTO clicks (ip, job_id) VALUES ('$ipa','$jid')";
          if ( $con->query($quip1) === TRUE) {
            echo json_encode($output,JSON_FORCE_OBJECT);
          }          
        }
        else {
            echo json_encode($output,JSON_FORCE_OBJECT);          
        }

        }
    }

Again: the result I receive seems to be empty. Even in the inspect-mode in Chrome I don't see values that I'd expect, making me suspicious that the $jid value wouldn't be delivered correctly. Btw: sender.id works so far. I used to test this.

  • 写回答

2条回答 默认 最新

  • dongtan2603 2018-06-01 21:35
    关注

    You aren't sending json or expecting json at server so remove:

    contentType: "application/json; charset=utf-8",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度