0

这让我很困惑——我不知道问题出在哪里!

此调用始终返回 500 错误:

询问:

$('body').on('click', '.day', function () {
    // a suspect day has been clicked
    if (confirm('Re-index documents received on this day?')) {
        // re-index
        var day = $(this).find('.day_number').text();
        var year = parseInt($('#hidYear').val());
        var month = parseInt($('#hidMonth').val());
        $.ajax({
            type: "POST",
            url: "ajax.asmx/ReIndexDay",
            data: JSON.stringify( { Month: month, Year: year, Day: day } ),
            contentType: "application/xml; charset=utf-8",
            dataType: "xml",
            success: function (data) {
                var calendarHTML = $(data).find(':first').text();
                // update hidden fields and calendar
                $('#hidYear').val(year);
                $('#hidMonth').val(month);
                $('#divContent').html(calendarHTML);
            },
            error: function (msg) {
                alert("Failed: " + msg.status + ": " + msg.statusText);
            }
        });
    }
});

C#

[WebMethod(Description = "Re-index the day and return HTML of a calendar table for the month")]
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public string ReIndexDay(int Day, int Month, int Year)
{
    Diagnostic.ReIndex(Day, Month, Year);
    return GetIndexCalendarHTML(Month, Year);
}

我现在被卡住了,所以所有建议都表示赞赏!

[编辑]

我从浏览器中得到了这个——不确定它是否准确,因为它可能不会复制同样的东西:

System.InvalidOperationException:请求格式无效:application/xml;字符集=UTF-8。在 System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 在 System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

[/编辑]

4

2 回答 2

1

In addition to setting the content type to:

contentType: 'application/json; charset=utf-8',

instead of XML like you had (as @Zachary said (and I said in a comment :)). You also need to actually send back XML.

Saying:

[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]

Doesn't actually encode your string as XML, all it does is set the Content-type header to:

Content-Type: text/xml; charset=utf-8. 

You actually have to return the XML. It's a bit misleading.

Edit: Actually, let me ammend that. It does XML except when you send a string. You can have it wrap your string in XML by doing this:

[ScriptMethod(ResponseFormat = ResponseFormat.Xml, XmlSerializeString=true)]

于 2012-04-25T15:41:12.140 回答
1

您发送 JSON,但您将内容类型设置为 XML。尝试将其更改为此。

 contentType: 'application/json; charset=utf-8',
于 2012-04-25T15:29:31.907 回答