Jquery.ajax report parseerror Invalid json error reason and workaround: Cannot parse

Source: Internet
Author: User
Tags parse error

(默认: 自动判断 (xml 或 html)) 请求失败时调用时间。参数有以下三个:XMLHttpRequest 对象、错误信息、(可选)捕获的错误对象。如果发生了错误,错误信息(第二个参数)除了得到null 之外,还可能是 "timeout" , "error" , "notmodified" "parsererror"

The first parameter returned by the error event XMLHttpRequest has some useful information: xmlhttprequest.readystate: Status Code  0-(uninitialized) has not yet called the Send () method  1- (load) called the Send () method, sending Request  2-(load complete) the Send () method executes, has received all response content  3-(interaction) is parsing the response content  4-(completion) Response content resolution is completed, you can call the
Xmlhttprequest.status Properties: Some error codes: "See below"

We know that JSON objects that are caused by invalid JSON errors cannot be resolved, and generally are errors in the syntax of the JSON string returned by the server. In this case, we only need to examine the JSON carefully to solve the problem.

Let's talk about the recent problem with using the $.ajax () method in using jquery 1.4 to resolve JSON objects.

The JSON object is:

[{name: ' Resale of secondary housing ', Infocount:0,pageurl: '/ershoufang '},{name: ' Buy-in-demand ', Infocount:0,pageurl: '/qiugou '},{name: ' Secondary housing decoration ', Infocount:0,pageurl: '/esfzhuangxiu '},{name: ' second-hand recycling ', Infocount:0,pageurl: '/huishou '},{name: ' Used motorcycles ', infocount:0, Pageurl: '/motor '},{name: ' Used car buy ', Infocount:0,pageurl: '/ERSHOUCHEQG '},{name: ' second-hand car transfer ', Infocount:9,pageurl: '/ Ershouche '},{name: ' Secondhand market ', Infocount:0,pageurl: '/ershoushichang '}]

I don't know if anyone has found this JSON object problem. This is not a problem with JavaScript scripts and jquery 1.4 versions. Because in 1.3 and earlier, jquery parses JSON objects through JavaScript's eval method. In 1.4, jquery used a more rigorous approach to parsing JSON. All content must use double quotation marks.

As you can see from Jquery.parsejson (JSON), Parsejson's explanation in the documentation is:

Accepts a JSON string that returns the parsed object.

Passing in a malformed JSON string throws an exception. For example, the following are malformed JSON strings:
{test:1} (test does not enclose double quotes)
{' Test ': 1} (using single quotation marks instead of double quotes)

Also, if you do not pass in anything, or an empty string, NULL, or Undefined,parsejson will return null.

All strings need to be extended with double quotes, numbers, Null,true,false.

For example:
Correct JSON string: "{\" result\ ": \" true\ ", \" msg\ ": \" delete succeeded \ "}"
Wrong JSON string: "{result:\" true\ ", msg:\" delete succeeded \ "}",: "{result:true, msg: Delete succeeded}", "{result: ' True ', msg: ' Delete succeeded '}"

The server returned the result as: {ID: "1", Data: "None"}

Engaged for a long time, I put the library back to 1.3.1 can be successful ~~lol, finally found that jQuery1.4.2 this version caused by the 1.4.2 in JQ to JSON parsing from the original eval to extremely strict $.parsejson () to deal with. I found it in the official documentation, as follows:

DataType ... "JSON": evaluates the response as JSON and returns a JavaScript object. In JQuery 1.4 The JSON data was parsed in a strict manner; Any malformed JSON are rejected and a parse error is thrown. (See json.org-Information on proper JSON formatting.)

http://api.jquery.com/jQuery.ajax/

That is to say, JQuery 1.4 has a strict requirement on the JSON data returned by the server, and must be in strict accordance with the JSON standard.

I will also summarize the $.parsejson's explanatory document below:

Jquery.parsejson (JSON)

Passing in a malformed JSON string would result in an exception being thrown. For example, the following is all malformed JSON strings:

{test:1} (test does not has double quotes around it). There must be double quotes {"Test" on both sides of test: 1}
{' Test ': 1} (' Test ' is using a single quotes instead of double quotes). Quotation marks on both sides of test cannot be single quotes, must be double quotes
Additionally if you pass in nothing, an empty string, null, or undefined, ' null ' is returned from Parsejson. Where The browser provides a native implementation of Json.parse, JQuery uses it to parse the string. For details on the JSON format, see http://json.org/.

Now everything is on the standard, we honestly follow the standard. Here are a few examples that meet the criteria:

{"MyValue": 1}
{"MyValue": "Red"}
{"MyValue": ["Black", 250]}

Some error codes:

1xx-Information Tips
These status codes represent a temporary response. The client should be prepared to receive one or more 1xx responses before receiving a regular response.
100-Continue.
101-Switch protocol.
2xx-success
This type of status code indicates that the server successfully accepted the client request.
200-OK. The client request was successful.
201-created.
202-accepted.
203-Non-authoritative information.
204-no content.
205-Reset the content.
206-Partial content.
3xx-redirection
The client browser must take more action to implement the request. For example, the browser might have to request a different page on the server, or repeat the request through a proxy server.
301-The object has been permanently moved, that is, permanent redirection.
302-The object has been temporarily moved.
304-not modified.
307-Temporary redirection.
4xx-Client Error
An error occurred and the client appears to be having problems. For example, a client requests a page that does not exist, and the client does not provide valid authentication information. 400-Bad request.
401-access is denied. IIS defines a number of different 401 errors, which indicate a more specific cause of the error. These specific error codes are displayed in the browser, but are not displayed in the IIS log:
401.1-Login failed.
401.2-server configuration caused logon failure.
401.3-not authorized due to ACL restrictions on resources.
401.4-Filter Authorization failed.
401.5-ISAPI/CGI application authorization failed.
401.7– access is denied by the URL authorization policy on the Web server. This error code is dedicated to IIS6.0.
403-Forbidden: IIS defines a number of different 403 errors that indicate a more specific cause of the error:
403.1-execution access is forbidden.
403.2-Read access is forbidden.
403.3-Write access is forbidden.
403.4-Requires SSL.
403.5-Requires SSL128.
The 403.6-IP address is rejected.
403.7-Requires a client certificate.
403.8-site access is denied.
403.9-Excessive number of users.
403.10-Invalid configuration.
403.11-Password change.
403.12-Deny access to the mapping table.
403.13-The client certificate is revoked.
403.14-Reject directory list.
403.15-Client access permission exceeded.
403.16-Client certificate is not trusted or invalid.
403.17-The client certificate has expired or is not yet valid.
403.18-The requested URL cannot be executed in the current application pool. This error code is dedicated to IIS6.0.
403.19-CGI cannot be executed for clients in this application pool. This error code is dedicated to IIS6.0.
403.20-passport Login failed. This error code is dedicated to IIS6.0.
404-not found.
404.0-(None) – No files or directories found.
404.1-Unable to access the Web site on the requested port.
The 404.2-web service extension lockout policy blocks this request.
The 404.3-mime mapping policy blocks this request.
405-The HTTP verb used to access this page is not allowed (method not allowed)
406-The client browser does not accept the MIME type of the requested page.
407-proxy authentication is required.
412-Precondition failed.
413– request entity is too large.
414-The request URI is too long.
415– media types not supported.
The range requested by 416– is not sufficient.
417– execution failed.
423– a locked error.
5xx-Server Error
The server could not complete the request because it encountered an error.
500-Internal server error.
The 500.12-application is busy restarting on the Web server.
The 500.13-web server is too busy.
500.15-Direct Request Global.asa is not allowed.
500.16–unc authorization credentials are incorrect. This error code is dedicated to IIS6.0.
The 500.18–url authorization store cannot be opened. This error code is dedicated to IIS6.0.
500.100-Internal ASP error.
501-The header value specifies the configuration that is not implemented.
An invalid response was received when the 502-web server was used as a gateway or proxy server.
The 502.1-cgi application timed out.
502.2-CGI Application error. Application.
503-The service is not available. This error code is dedicated to IIS6.0.
The 504-gateway timed out.
505-http version is not supported.
Ftp
1xx-a definite initial reply
These status codes indicate that an operation has started successfully, but the client wants to get another reply before continuing with the new command.
110 Restart Tag reply.
120 service is ready, starting in nnn minutes.
125 The data connection is open and the transfer is starting.
150 file status OK, ready to open data connection.
2xx-sure to complete the reply
An operation has been successfully completed. The client can execute the new command. 200 command OK.
202 commands are not executed and there are too many commands on the site.
211 System Status, or system help reply.
212 Directory status.
213 file status.
214 Help message.
215NAME system type, where name is the official system name listed in the Assignednumbers document.
220 The service is ready to execute the request of the new user.
221 Service closes the control connection. If appropriate, please log out.
225 Data connection Open, no in-progress transfer.
226 Close the data connection. The requested file operation was successful (for example, transferring a file or discarding a file).
227 Enter passive mode (H1,H2,H3,H4,P1,P2).
230 The user is logged in and continues.
250 The requested file operation is correct and completed.
257 "PATHNAME" has been created.
3xx-affirmative in the middle reply
The command was successful, but the server needs more information from the client to complete the processing of the request. 331 user name is correct, password required.
332 You need to log in to your account.
350 The requested file operation is waiting for further information.
4xx-Transient negation Completion reply
The command is unsuccessful, but the error is temporary. If the client retries the command, it may execute successfully. 421 service is not available and the control connection is shutting down. If the service determines that it must be closed, this response is sent to any command.
425 The data connection cannot be opened.
426connectionclosed;transferaborted.
450 The requested file operation was not performed. The file is not available (for example, the file is busy).
451 requested operation aborted: Local error is being processed.
452 the requested operation was not performed. The system does not have enough storage space.
5xx-permanent negation of complete reply
The command is unsuccessful and the error is permanent. If the client retries the command, the same error will occur again. 500 syntax error, command not recognized. This may include errors such as the command line being too long.
501 There are syntax errors in the arguments.
502 command not executed.
503 wrong sequence of commands.
504 command not executed for this parameter.
530 not logged in.
532 The account is required to store the file.
550 the requested operation was not performed. The file is not available (for example, the file was not found and no access was granted).
551 requested operation aborted: unknown page type.
552 requested file operation terminated abnormally: storage allocation exceeded (for current directory or dataset).
553 The requested operation was not performed. The file name is not allowed.
Common FTP status codes and their causes
The 150-FTP uses two ports: 21 for sending commands, and 20 for sending data. Status code 150 indicates that the server is ready to open a new connection on port 20 and send some data.
226-The command opens a data connection on port 20 to perform operations, such as transferring files. The operation completed successfully and the data connection is closed.
230-The status code is displayed after the client sends the correct password. It indicates that the user has successfully logged on.
331-The status code is displayed after the client sends the user name. The status code is displayed regardless of whether the provided user name is a valid account in the system.
426-The command opens the data connection to perform the operation, but the operation has been canceled and the data connection is closed.
530-The status code indicates that the user cannot log on because the user name and password combination is not valid. If you are logged on with a user account, you might type an incorrect user name or password, or you may choose to allow only anonymous access. If you log on with an anonymous account, the configuration of IIS may deny anonymous access.
550-The command was not executed because the specified file is not available. For example, the file you want to get does not exist, or you try to put the file into a directory where you do not have write permission.

Jquery.ajax report parseerror Invalid json error reason and workaround: Cannot parse

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.