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 ~ _~