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条)

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示