Recommended return type for web service that returns nothing (called with jquery)

I have a web service with some methods, they technically do not do much on the client. I call it by jquery / ajax

What is the recommended return type, I would usually return JSON ... but in case it returns nothing ... do I return a boolean? i.e. true = succesful ... and false = error ??

Any ideas?

+3
source share
5 answers

What about HTTP status codes:

200 OK
500 Error
+3
source

If it does not return anything, you should use the HTTP status code:

204 No Content
The server successfully processed the request, but is not returning any content.

HTTP , HTTP, .

JSON ( SOAP XML , ). Stack Overflow, , HTTP-:

{ "response": true, "data" : <data>, "message" : <message> }

response , . null, , .

HTTP, HTTP, .

5xx

500 Internal Server Error
501 Not Implemented
502 Bad Gateway
503 Service Unavailable
504 Gateway Timeout
505 HTTP Version Not Supported
506 Variant Also Negotiates (RFC 2295 )
507 Insufficient Storage (WebDAV) (RFC 4918 )
509 Bandwidth Limit Exceeded (Apache bw/limited extension)
510 Not Extended (RFC 2774 )

2xx

200 OK
201 Created
202 Accepted
203 Non-Authoritative Information (since HTTP/1.1)
204 No Content
205 Reset Content
206 Partial Content
207 Multi-Status (WebDAV)
+2

SOAP XML.

.

<m:IsYourMethodSuccessful xmlns:m="Some-Namespace-URI"> 
    <return>true</return> 
</m:IsYourMethodSuccessful> 

, , , .

, json:

{ success : true }

{ success : false, exception : { ... } }
+1

- , .

0

. . , .

0

Source: https://habr.com/ru/post/1709575/


All Articles