FAQ about json format data returned by ashx in Ajax request, ashxjson

Source: Internet
Author: User

FAQ about json format data returned by ashx in Ajax request, ashxjson

 

Problem: the string json format returned by ashx fails to be automatically parsed by ajax at the front end.

Problem Analysis: After troubleshooting, it is found that "'" single quotation marks appear when splicing json. jquery cannot be parsed. You can use "Double quotation marks. For example:

String strjson = "[{'username': 'test'}]"; // single quotation marks make jquery unable to be automatically parsed. String strjson = "[{\" userName \ ": \" test \ "}]"; // double quotation marks can be parsed;

 

Ajax code:

$. Ajax ({type: 'post', url: 'report. ashx? Type = 1', dataType: 'json', async: 'true', // asynchronous cache: 'false', success: function (data) {mychartOptions. series = data; var chart = $ ("# report1 "). highcharts (mychartOptions);}, error: function (XMLHttpRequest, textStatus, errorThrown) {$ ("# report1" 2.16.html ("<span> failed to retrieve data" + textStatus + "</span> ");}});

 

Background ashx code:

Public void ProcessRequest (HttpContext context) {string type = context. request ["type"]; if (type = "1") {// all three settings pass the test, if contenType is not set, you can also "application/json" "text/plain" "text/json" // context. response. contentType = "text/json"; string json = "[{\" userid \ ": 123}]"; context. response. write (json);} else if (type = "2") {GetReport2 (context );}}

 


Jquery + ajax + json + ashx returns data in json format

If your ajax code can access the background to obtain data, there is no problem accordingly. If an error occurs, it is in the format of your ajax data retrieval, when debugging, check whether the data you obtain is in json format.
You can comment out the dataType: "json" line of code and enter success.

No data is returned from ashx data.

In your ajax request, the returned data type is set to "json", which indicates that the expected response MIME type of the script is text/json, And the fatal point is, jQuery converts your response text to a json object. your response is actually a plain text and does not comply with the json specification. this will throw an exception interrupt program in Some browsers, so your success callback function is not executed.
If you change the code in the response to {"test": "OK"}, or set the ajax ype of the page to "text ", alternatively, you can simply remove the dataType parameter (that is, the "json" // return the json type data) so that jQuery can intelligently guess that you should be able to get the response value smoothly.

This is only one of the cases where the response cannot be obtained smoothly. If the problem persists, ask. 3q ~ _~

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.