简体   繁体   中英

Ajax requests but doesn't get an answer form PHP script

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.

你不是在服务器上发送json或期望json所以删除:

contentType: "application/json; charset=utf-8",

There might be several issues. One common problem is when the request is done from a different domain the server does not serve the request. Use header('Access-control-allow-origin: *') at the start of the file or the function

Use CORS carefully

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM