当POST工作完美时,无法让GET工作

时间:2013-07-02 09:31:01

标签: c# jquery .net-3.5 asmx

我有以下代码,如果我使用POST,它会完美运行。但是,由于各种原因,我需要让它与GET一起使用:(我已添加注释以显示我所做的3个简单更改,请参阅客户端脚本中的CHANGE 1和CHANGE 2,以及服务器端脚本中的CHANGE 3):

客户端:

function selectedDateTime(strDate, strHours, strMinutes) {

    $.ajax({
        url: 'webservice.asmx/GetCount',
        //type: 'POST', // CHANGE 1 - THIS WAS POST
        type: 'GET',
        //data: '{"theDate": "' + strDate + ' ' + strHours + ':' + strMinutes + ':00"}', // CHANGE 2 - REMOVED THE CURLY BRACKETS
        data: '"theDate": "' + strDate + ' ' + strHours + ':' + strMinutes + ':00"',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        processData: false,
        success: function(department) {
            console.log("success: " + department.d); 
        },
        error: function(xhr, status, error) {
            console.log("status message: " + status);
            console.log("error message: " + error);
            console.log("xhr message: " + xhr.responseText);
        }
    });

}

服务器侧

[WebMethod()]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] // CHANGE 3 - ADDED THIS LINE TO FORCE A GET
public double GetCount(string theDate)
{
    string[] strDateAndTime = theDate.Split(' ');

    string[] strStartDateParts = strDateAndTime[0].Split('/');
    string[] srtStartTimeParts = strDateAndTime[1].Split(':');

    int year = Int32.Parse(strStartDateParts[2]);
    int month = Int32.Parse(strStartDateParts[1]);
    int day = Int32.Parse(strStartDateParts[0]);
    int hour = Int32.Parse(srtStartTimeParts[0]);
    int min = Int32.Parse(srtStartTimeParts[1]);
    int sec = Int32.Parse(srtStartTimeParts[2]);

    DateTime meetingDate = new DateTime(year, month, day, hour, min, sec);

    using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
    {

        using (command = new SqlCommand("intranet.dbo.BusinessHours", connection))
        {

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@meeting_date", SqlDbType.DateTime).Value = meetingDate;

            connection.Open();

            using (reader = command.ExecuteReader())
            {
                reader.Read();
                return (double)reader["hours"];
            }
        }
    }
}

错误讯息:

我使用Google Chrome的开发者工具来提取此错误消息。

GET http://intranet/webservice.asmx/GetCount?%22theDate%22:%20%2201/07/2013%2013:00:00%22 500 (Internal Server Error) 
status message: error 
error message: Internal Server Error 
xhr message: {"Message":"Invalid web service call, missing value for parameter: \u0027theDate\u0027.","StackTrace":"   at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

问题:

当使用POST完美运行时,任何人都知道为什么会这样。我只是想做同样的事情,但需要使用GET代替。

2 个答案:

答案 0 :(得分:1)

问题是您将JSON作为查询字符串传递。它适用于POST,但不适用于GET。 对于你需要的GET

 data: 'theDate=' + strDate + ' ' + strHours + ':' + strMinutes + ':00'

答案 1 :(得分:1)

var date =  '"' +strDate + ' ' + strHours + ':' + strMinutes + ':00"';
$.ajax({
        url: 'webservice.asmx/GetCount?theDate=date,
        type: 'GET', 
        dataType: 'json',
        processData: false,
        success: function(department) {
            console.log("success: " + department.d); 
        },
        error: function(xhr, status, error) {
            console.log("status message: " + status);
            console.log("error message: " + error);
            console.log("xhr message: " + xhr.responseText);
        }
    });