search
Homephp教程php手册PHP如何传递Ajax成功或失败的状态?HTTP Status Codes可以实现

一般处理Ajax 回应时会传送的信息种类有:数据、成功信息、错误信息、失败信息以及处理状态,传递的信息种类并不一致,再加上除了数据之外,通常还希望能传递处理状态,这种情况大部分会选择是以JSON 的方式传递这两个信息,以下是常见的几种格式:

但以执行状态跟操作行为作一个归纳,可以区分以下几种传递结果:

{ code: 1, msg: "OK" } { success: true , result: "data" , errorMsg: "" } { status: 'success' , result: [], errorMsg: "" } //...

但以执行状态跟操作行为作一个归纳,可以区分以下几种回传结果:

数据操作 HTTP Method 成功 错误/失败

读取(Read) GET 数据 错误/失败信息

新增 (Create)  POST 成功信息 错误/失败信息

修改 (Update)

删除 (Delete)

从上面的归纳可以看出规律性,接着只要有方法可以传送处理的状态,以及能够区分数据的种类,其实就单纯很多,而HTTP Status Codes 就是用来传递HTTP 的处理状态,如果利用这个方式来传递自定义的处理状态,这样HTTP Content 就可以很单纯传递数据,让数据格式不受限于JSON,还可以使用其他格式(text, xml, html),而且XMLHttpRequest 本身就有处理HTTP Status Codes 的能力,而jQuery.ajax 也有提供error status 的处理,所以可以利用这个来定义状态的处理,在HTTP Status Codes 有几个已经定义状态,很适合用来传递处理状态的信息:

400Bad Request错误的请求适用在表单内容的错误,如必填栏位未填、Email 格式错误

403Forbidden没有权限,被禁止的适用在没有登录或权限不足

500Internal Server Error内部服务器错误适用在程序的错误

jQuery 接收信息的范例,代码如下:

$.ajax({     
   type: "POST" ,     
   url: document.location,     
   success: function (data, textStatus, jqXHR) {     
   alert(data);     
   },     
   error: function (jqXHR, textStatus, errorThrown) {     
   alert(jqXHR.responseText);     
   }     
});

 

PHP 传递错误信息的范例,代码如下:

if (php_sapi_name() == 'cgi' ){     
   header ( "Status: 400 Bad Request" );     
} else {     
   header ( "HTTP/1.0 400 Bad Request" );     
}
exit ( "储存失败!!" );

C# MVC 传递错误信息的范例,代码如下:

Response.TrySkipIisCustomErrors = true ;    
Response.StatusCode = 400;    
return Content( "储存失败!!" );


文章链接:

随便收藏,请保留本文地址!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.