次のコードを使用すると、完全に機能しますPOST
。ただし、さまざまな理由で動作させる必要がありGET
ます。脚本):
クライアント側:
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
代わりに使用する必要があります。