问题:
我有以下用于 jQuery 可排序的 JavaScript 处理程序。
问题是我得到一个错误(parseerror)。
问题是 datatype: JSON + contentType: 'application/json' 似乎需要正确发送数据。
但是,我不希望响应必须是 JSON 格式。我想要对应用程序/json 请求的文本/简单响应。
那不可能吗?
或者:我该如何正确地做到这一点(无需提供 JSON 响应)?
Http处理程序:
Imports System.Web
Imports System.Web.Services
Public Class FMS_SaveTeaserSort
Implements System.Web.IHttpHandler
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim strpagesget As String = context.Request.QueryString("pages")
Dim strPagesPost As String = context.Request.Form("pages")
Dim json As String = Nothing
Using sr As New System.IO.StreamReader(context.Request.InputStream)
json = sr.ReadToEnd()
End Using ' sr
json = context.Server.UrlDecode(json)
'Dim customer As Byte() = New Byte(System.Convert.ToInt32(context.Request.InputStream.Length) - 1) {}
'context.Request.InputStream.Read(customer, 0, customer.Length)
'Dim customerJSON As String = System.Text.Encoding.UTF8.GetString(customer)
' TODO: deserialize the JSON back to a Customer object
context.Response.ContentType = "text/plain"
context.Response.Write("Hello World!")
End Sub
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
JavaScript 代码:
$(function () {
//$('#sortable').sortable();
$('#sortable').sortable({
update: function (event, ui) {
var strMovedElementId = ui.item.attr('id');
//alert("post");
$.ajax({
url: './ajax/FMS_SaveTeaserSort.ashx'
, type: 'POST'
, cache: false
//, contentType: 'application/json'
//, contentType: 'text/plain'
//, contentType: "application/x-www-form-urlencoded"
, contentType: "application/json; charset=utf-8"
, dataType: "json"
//, data: JSON.stringify({ pages: $(this).sortable('toArray') })
, data: { pages: $(this).sortable('toArray') }
, success: function (result) {
//alert("success");
}
, error: function (request, type, errorThrown) {
var message = "There was an error with the AJAX request.\n";
//alert(type);
switch (type) {
case 'timeout':
message += "The request timed out.";
break;
case 'notmodified':
message += "The request was not modified but was not retrieved from the cache.";
break;
case 'parsererror':
message += "Parser error: Bad XML/JSON format.";
break;
default:
message += "HTTP Error (" + request.status + " " + request.statusText + ").";
}
message += "\n";
alert(message);
switch (request.status) {
case 400:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe request cannot be fulfilled due to bad syntax');
break;
case 401:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nAuthentication is possible but has failed or not yet been provided');
break;
case 403:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nAccess denied');
break;
case 404:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe requested page could not be found but may be available again in the future');
break;
case 405:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nA request was made of a page using a request method not supported by that page');
break;
case 408:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe server timed out waiting for the request');
break;
case 410:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe requested page is no longer available');
break;
case 500:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nA generic error message, given when no more specific message is suitable\nError description:\n' + request.responseText);
break;
case 502:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe server was acting as a gateway or proxy and received an invalid response from the upstream server (fastcgi-crash?)');
break;
case 503:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe server is currently unavailable (overloaded or down)');
break;
case 504:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe server was acting as a gateway or proxy and did not receive a timely response from the upstream server');
break;
case 511:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nThe client needs to authenticate to gain network access');
break;
case 0:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nRequest aborted');
break;
default:
alert('HTTP ' + request.status + ': ' + request.statusText + '\nUnknown error: \nError description:\n' + request.responseText);
} // End Switch
}
, complete: function (result) {
//alert("complete");
}
}); // End ajax
} // End Update
}); // End Sortable
$('#sortable').disableSelection()
});