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

查看:258
本文介绍了我可以更改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范围内有一组错误,但如果你不想麻烦,只是使用一个500通用的错误代码响应。



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



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

  show_error('Howdy,this is my debug message',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
警报('Ooops,发生了:'+ textStatus +''+ errorThrown);
}
});

如果你正在使用任何其他工具包甚至DOM对象,你仍然可以捕捉那些,因为他们是只需 XMLHttpRequest对象,并且您的工具箱可能会回调HTTP错误响应或成功



因为它遵循标准,它的更容易调试,您将该工作委派给show_error()帮助器,这是有原因的,最重要的是因为所有很酷的孩子正在使用它



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



这是正确的,因为当你在jquery的错误回调中捕获请求时,你得到的是一般的错误描述和类似内部服务器错误和500但是,你仍然有一个漂亮的html响应与您的自定义调试消息,看到它只是使用某种开发工具为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天全站免登陆