可以更改CodeIgniter show_error函数来返回一个JSON对象吗? [英] Can I change the CodeIgniter show_error function to return a JSON object?

查看:226
本文介绍了可以更改CodeIgniter show_error函数来返回一个JSON对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CI开发一个完整的Ajax应用程序,并希望show_error函数返回JSON数据,通过我自己的JS代码进行调试。有没有本地或简单的方法来做,还是需要改变show_error函数?如果需要,它可以被覆盖?

解决方案

如果您希望返回复杂的错误,那么这个解决方案可能并不完全有用对象像数组(虽然你可以,但是你必须在客户端上解析json),但是这里我们去:



只需使用HTTP



为什么?那么你可以传递一个第二个参数,这恰好是HTTP响应代码,这恰好是很酷的,因为它允许你使你的应用程序感知HTTP,并且使用客户端的ajax请求非常好。 / p>

您想要做的是首先定义在后端可能发生什么样的错误,列出HTTP错误代码这里



HTTP错误代码



很可能您将使用200,400和500范围内的错误代码。实际上,当您在网络浏览器上点击服务器时,通常会收到200个HTTP响应代码,这意味着一切顺利。



您是否看到这些内部服务器错误消息?那么它们是500个http响应代码。这就意味着这是服务器的错误。哪一个?这取决于你如何对它们进行分类,在500范围内有一组错误,但是如果你不想麻烦,只需使用一般的错误代码响应。



另一个范围是400.那些通常是用户错误,例如,如果你去一个服务器内的url,它不存在你会得到着名的404没有找到,400是一个通用的错误代码,意思是客户端(在这种情况下,浏览器)请求了一些东西,但请求无效,在404的情况下,您请求的资源未被发现,这是客户端错误,因为您应该知道哪些资源可用服务器。



如何在codeigniter中执行



这是非常简单的其实。如果您在文档中看到 show_error()参考,则表示该方法接收到第一个参数作为错误消息,另一个可选,它接收错误代码。哪个错误代码?以前我们讨论的HTTP代码,所以:

  show_error('Howdy,这是我的调试消息',500); 

将向客户端发送一个500 HTTP响应代码,包括您的消息。



如何在AJAX中捕获



考虑到您正在使用jQuery,这是您通常会做的: / p>

  $。ajax({
type:'POST',
url:example.com/resource,
data:$(#some-form)。serialize(),
dataType:'json',
success:function(data,textStatus,req){
/ /做一些数据,这是一个从PHP
}返回的json对象的数据,
错误:function(req,textStatus,errorThrown){
//当你发送不同的东西时,会发生这种情况从200 OK HTTP
alert('Ooops,发生了一些事情:'+ textStatus +''+ errorThrown);
}
});

如果您直接使用任何其他工具包甚至DOM对象,那么仍然可以捕获它们,因为它们是只需 XMLHttpRequest对象,您的工具包有可能对HTTP错误响应或成功响应进行回调。



为什么要关心?



由于遵循标准,易于调试,那个工作到show_error()帮助器,这是因为一个原因,最重要的是因为所有的很酷的孩子都在使用它



很酷,但等待,我看不到我的自定义错误消息!



这是正确的,因为当您在jquery的错误回调中捕获请求时,您得到的是通用错误描述和Internal server error(内部服务器错误)和500的代码,但是您仍然有一个漂亮的html响应wi您的自定义调试消息,看到它只是使用某种开发工具的Firefox或chrome。例如,如果您使用Google Chrome,您可以打开开发人员工具:



转到网络标签,您会看到HTTP请求,点击其名称





您将看到详细信息和自定义错误消息与通常的CI模板,这是您的请求中的消息返回的HTML





最后如果要进一步挖掘和调试正是从php / web服务器发送到客户端到头文件选项





免责声明: 截图未从生产服务器获取:)


I'm developing a full Ajax app with CI and want the show_error function to return JSON data, to debug trough my own JS code. Is there any native or simple ways to do that or will I need to change show_error function? If needed, it can be overridden?

解决方案

Well maybe this solution isn't entirely useful if you are looking to return complex error objects like arrays (although you could, but you'd have to parse json on the client) but here we go:

Just use HTTP

Why? Well, you can pass a second parameter to it, which happens to be the HTTP response code, which happens to be cool, cause it allows you to make your application HTTP-aware and that works amazingly good with client-side ajax requests.

What you want to do is first define what kind of errors can happen on the backend, there is a list of HTTP error codes here.

HTTP error codes

Most likely you'll use the error codes in the 200, 400 and 500 ranges. Actually when you hit a server on your web browser it would normally receive a 200 http response code which mean everything went fine.

Have you seen those "Internal server eror" messages? Well they are 500 http response codes. And that means exactly that, it was an error from the server. Which one? It depends how you categorize them, there's a set of errors on the 500 range, but if you don't want to hassle with that just use a 500 generic error code response.

The other range is 400. Those are usually error from users, for example if you go to a url inside a server and it does not exist you'd get the famous 404 not found, 400 is a generic error code meaning that the client (in this case, the browser) requested something but the request was invalid, in the case of a 404 specifically, that the resource you requested was not found, it is a client error because you are supposed to know which resources are available on the server.

How to do it in codeigniter

It is extremely simple actually. If you see the show_error() reference on the documentation it states that the method receives a first parameter as the error message, and a second, optional, that receives an error code. Which error code? The HTTP codes we talked about before, so:

show_error('Howdy, this is my debug message', 500);

Would send a 500 HTTP response code to the client, including your message.

How to catch in AJAX

Considering you are using jQuery this is what you would normally do:

$.ajax({
    type: 'POST',
    url : example.com/resource,
    data: $("#some-form").serialize(),
    dataType: 'json',
    success : function(data, textStatus, req) {
        //do something with data which is a json object returned from PHP
    },
    error: function(req, textStatus, errorThrown) {
        //this is going to happen when you send something different from a 200 OK HTTP
        alert('Ooops, something happened: ' + textStatus + ' ' +errorThrown);
    }
});

If you were using any other toolkit or even the DOM object directly you can still catch those since they are simply XMLHttpRequest objects and chances are your toolkit has a callback for either an HTTP error response or a success response.

Why would I care?

Because it follows standards, its easier to debug, you delegate that work to the show_error() helper which is there for a reason and most importantly because all the cool kids are using it.

Cool, but wait, I don't see my custom error message nowhere!

That is right, because when you catch the request in the error callback of jquery what you get is the generic error description and the code like "Internal server error" and 500 respectively, however, you still got a pretty html response with your custom debug message, to see it just use some sort of developer tool for firefox or chrome. For example, if you use google chrome you can open the developer tools:

Go to network tab and you'll see the HTTP request, click on its name

You'll see the details and your custom error message with the usual CI template, this was the html returned with your message inside the request

Finally if you want to dig further and debug exactly what was sent from php/web server to the client go to the headers option

Disclaimer: Screenshots were not taken from a production server :)

这篇关于可以更改CodeIgniter show_error函数来返回一个JSON对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆