我可以改变codeIgniter show_error函数返回的JSON对象?函数、对象、codeIgniter、show_error

2023-09-10 14:56:53 作者:故事与酒

这是一个简单的情况下,我发展与CI一个完整的AJAX应用程序,并希望show_error函数返回JSON数据,来调试槽我自己的JS code。是否有任何本地或简单的方法来做到这一点,或我需要改变show_error功能?如果需要,它可以覆盖?

This is a simple situation, 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 i will need to change show_error function? if need, it can be override?

推荐答案

嗯,也许这个解决方案并不完全是有用的,如果你正在寻找返回复杂的错误对象和数组一样(虽然你可以,但你必须解析JSON在客户端上),但在这里我们去:

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:

只要使用HTTP

为什么呢?好了,你可以传递第二个参数给它,这恰好是HTTP响应code,这恰好是很酷的,因为它可以让你让你的应用程序的HTTP感知,并且工作得非常良好,客户端AJAX请求。

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.

您想要做的是先定义可以在后台发生什么样的错误,有HTTP错误codeS的这里。

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错误codeS

您很有可能会使用错误codeS在200,400和500的范围。其实,当你打一个服务器上的Web浏览器,它通常会收到一个200 HTTP响应code这意味着一切都很好。

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.

你见过那些内部服务器eror的消息?那么,他们是500 HTTP响应codeS。这意味着正是这样,它是从服务器的错误。哪一个?这取决于你如何分类这些,还有一组对500范围内的错误,但是如果你不想麻烦地,仅仅用500一般性错误code响应。

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.

另外范围是400。这些通常是从用户的错误,例如,如果你去到一个URL服务器内部,它不存在,你会得到著名的404未找​​到,400是一般性错误$ C $ Ç这意味着客户端(在这种情况下,浏览器)要求的东西,但该请求是无效的,在404的情况下,具体而言,未找到您请求的资源,它是一个客户端的错误,因为你应该知道哪些资源可在服务器上。

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.

如何做到这一点在codeigniter

这是非常简单的实际。如果你看到 show_error()上的文档它参考指出该方法接收第一参数作为错误消息,和一个第二,可选的,其接收误差code。哪个错误code?在HTTP codeS我们谈到过,所以:

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);

将派遣一个500 HTTP响应code到客户端,包括你的消息。

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

如何捕捉在AJAX

考虑到你所使用的jQuery这是你通常会做:

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);
    }
});

如果您正在使用的任何其他工具,甚至DOM对象直接,你仍然可以捕捉那些因为他们根本的 XMLHtt $ 和机会是你的工具箱有一个回调是HTTP错误响应或成功响应p $ pquest对象。

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.

我为什么要在乎?

由于它遵循的标准,它更容易调试,你的工作委托给show_error()辅助这是有原因的,最重要的,因为所有的时尚的年轻人都在使用它。

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.

酷,但等待,我不认为我的自定义错误消息不通!

这是正确的,因为当你抓住jQuery中的错误回调的要求,你得到的是一般错误的描述和code,如内部服务器错误和500分别,但是,你仍然有$与您的自定义调试消息p $ ptty的HTML响应,看它只是使用某种开发工具,Firefox或Chrome。例如,如果你使用谷歌浏览器可以打开开发者工具:

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:

进入网络选项卡,你会看到的HTTP请求,单击其名称

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

您会看到的细节,并与通常的CI模板的自定义错误消息,这是您的留言返回请求中HTML

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

最后,如果你想进一步和调试挖掘正是从PHP / Web服务器发送到客户端去头选项

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

免责声明: 截图不采取从生产服务器:)的