Ajax + PHP usage and the problems met

Because it is a php white, so the new knowledge must constantly test themselves and explore.

Under share their interaction with php + ajax usage and problems.

 

Front-end code:

  
$.ajax({
type: "POST",
dataType: "json",
data: {"data": "null"},
url: "{:url('@index/index')}",
async: false, // true is an asynchronous request, false synchronous request
success: function (msg) {
console.log(msg.ajax_data);
$("#hnssc .next_date font").html(msg.ajax_data);
},
error: function () {
console.log("error");
}
});
 
Back-end code:
$data104 = Db::name('data_time')->where("actionTime > DATE_FORMAT(NOW(),'%H:%i:%s')")->order('actionNo asc')->where('type', '127')->limit(1)->select();
$arr = array();
$arr['ajax_data'] = $data104[0]['actionTime'];
// $arr['ajax_data'] = '22:00:00';
echo json_encode($arr);
 
Then, after all, it is a white thing, there are other ways temporarily fixed, slowly groping.
 
 
problem:
 
1, is returned after the successful data string format is the front-end code dataType: 'json' not set or set wrong
 
2, issue into the error, failed to enter the function should be more, my side of the problem is that the front end of the incoming and outgoing offspring inconsistent data formats, so do json resolution.
json_encode, this method. Specific usage is not very clear, self-Baidu.
 
Note: ajax consistent data format to the front end and the rear end of the return data format, parsing the backend data attention to the desired data format.
 
 
I forgot to point out an error, please mouth mercy.
 

Guess you like

Origin www.cnblogs.com/showcase/p/11037263.html