简体   繁体   English

Ajax处理数据库错误

[英]Ajax Handling Database Errors

This may be more of an opinion based answer but I would like to know which method is more efficient/faster for handling database errors through an ajax call. 这可能更多是基于意见的答案,但是我想知道哪种方法通过ajax调用来处理数据库错误更有效/更快。 Both of these errors would be handled in the success function of the ajax call. 这两个错误都将在ajax调用的成功函数中处理。

method 1: 方法1:

<?php
header("dberror:" . $errormsg );
exit();
?>

method 2: 方法2:

<?php
echo json_encode(array("dberror" => $errormsg));
?>

for example i dont show any errors on JSON i simply do a transaction message is success or had an error example 例如,我没有在JSON上显示任何错误,我只是做一个事务消息成功或有错误示例

  { "transaction":"success" }

  { "transaction":"error" }

and also you need to setup a json header. 并且您还需要设置一个json标头。

 header('application/json');

 echo json_encode($json_string); 

I think the json method is better for parsing. 我认为json方法更适合解析。 With jquery you can do an ajax call specially for json. 使用jquery,您可以专门针对json进行ajax调用。 The call is $.getJSON (). 调用是$ .getJSON()。 I have never seen to return an error as header ... 我从未见过返回错误作为标题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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