2013-07-02 37 views
1

我有以下代码,如果我使用POST,那么该代码完美。但是,我需要让它与GET一起工作,其原因有很多:(我已添加注释以显示我所做的3个简单更改,请参阅客户端脚本中的CHANGE 1和CHANGE 2,以及服务器端脚本中的CHANGE 3)端脚本):当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

+0

查询字符串中添加资料,一边进行GET请求 –

+0

查询字符串确实有数据不是吗?请参阅错误消息的第一行。 – oshirowanen

+0

你正在尝试传递查询字符串中的json对象...服务器ll'无法理解该格式的请求 –

回答

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); 
     } 
    }); 
+0

基于此,我收到以下错误消息:'GET http://intranet/webservice.asmx/GetCount?theDate = 01/07/2013%2013:00:00%22 500(内部服务器错误)'和'xhr message:{“Message”:“无效的JSON原语:07/2013 13:00:00 \”。“,' – oshirowanen

+0

得到它的工作,它应该是'var date ='”'+ strDate +''+ strHours +':'+ strMinutes +':00“';',thank.s – oshirowanen

1

问题是您传递JSON作为查询字符串。它在POST中工作,但不在GET中。 为得到你所需要

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

你好,我已经更新了我的问题基于此,因为即使进行此更改后错误仍然存​​在。请参阅最新的问题。 – oshirowanen

相关问题