For some reason, AJAX does not work.

JS: (instead of ip.ip.ip.ip there is a normal IP, of course).

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" ></script> <script type="text/javascript"> $(document).ready(function () { var validateStatus = $('#validateStatus'); $('#status').blur(function() { var t = this; validateStatus.removeClass('error').removeClass('success').html('<img src="/view/images/ajax.gif" height="16" width="16" /> update ...'); var td = t.parentNode; var ID_of_TD = td.id; $.ajax({ url: 'http://ip.ip.ip.ip/src/util/ajax.php', data: 'action=setStatus&status=' + t.value + '&id=' + ID_of_TD, dataType: 'json', type: 'post', success: function (j) { if(j.ok){ validateStatus.html('<img src="http://ip.ip.ip.ip/view/images/accept.png"/>').removeClass('error').addClass('success'); } else{ validateStatus.html('<img src="http://ip.ip.ip.ip/view/images/exclamation.png"/> '+j.msg).removeClass('success').addClass('error'); } } }); }); }); </script> 

HTML

 <td id="557144"><input type="text" id="status" name="status" maxlength="30" required /><span id="validateStatus"></span><br/></td> 

PHP:

(one)

 if (@$_REQUEST['action'] == 'setStatus' && isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { echo json_encode(setStatus($_REQUEST['status'], $dbh)); exit; } 

(2)

 function setStatus($id, $status, $dbh) { $response = array(); $dao = new UserDao($dbh); if ($dao->setStatusUser($id, $status, $dbh)) { $response = array( 'ok' => true, 'msg' => ""); } else { $response = array( 'ok' => false, 'msg' => "Wrong."); } return $response; } 

(3)

  public function setStatusUser($id, $status){ $stmt = $this->dbh->prepare("UPDATE `vk_users` SET `status`= :status WHERE `vkid`= :id"); $stmt->bindParam(':id', $id, PDO::PARAM_STR, 250); $stmt->bindParam(':status', $status, PDO::PARAM_STR, 250); $stmt->execute(); $count = $stmt->fetchColumn(); return $count; } 
  • Nothing really is clear from the code hassle, but one thing is clear - the security policy prohibits creating AJAX requests to other domains, subdomains and another port. Therefore ... Use the hack, Valentina! - user31688
  • This is the same domain / IP. I just feel uncomfortable writing without an IP address. - Valentina
  • And increase the likelihood of failure of something? If the address to the same domain, it is better to remove the absolute path and use the root /path/to/file from the root. - user31688
  • @Valentina sit and wonder why it doesn’t work, of course, you can, but it’s wiser to add an error handling request to Ajax: error: function(jqXHR jqXHR, String textStatus, String errorThrown) { ... } ( documentation ) and find out what is wrong . - Regent
  • @TheDoctor, the sarcasm is nice, but the code is given in the question and the person is trying to figure out the problem. If this question is bad, then why is SO needed at all? - hardsky

1 answer 1

Try processing the received response with the jQuery.parseJSON() function or setting a header for the php file header('Content-type: application/json; charset=utf-8');

 ... success: function (j) { var j = $.parseJSON(j); if(j.ok){ validateStatus.html('<img src="http://ip.ip.ip.ip/view/images/accept.png"/>').removeClass('error').addClass('success'); } else{ validateStatus.html('<img src="http://ip.ip.ip.ip/view/images/exclamation.png"/> '+j.msg).removeClass('success').addClass('error'); } } ...