Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';当POST工作正常时,我就不能上班了_C#_Jquery_.net 3.5_Asmx - Fatal编程技术网

C# Can';当POST工作正常时,我就不能上班了

C# Can';当POST工作正常时,我就不能上班了,c#,jquery,.net-3.5,asmx,C#,Jquery,.net 3.5,Asmx,我有下面的代码,如果我使用POST,它可以很好地工作。但是,出于各种原因,我需要让它使用get(我添加了注释以显示我所做的3个简单更改,请参见客户端脚本中的更改1和更改2,以及服务器端脚本中的更改3): 客户端: function selectedDateTime(strDate, strHours, strMinutes) { $.ajax({ url: 'webservice.asmx/GetCount', //type: 'POST', // CH

我有下面的代码,如果我使用
POST
,它可以很好地工作。但是,出于各种原因,我需要让它使用
get
(我添加了注释以显示我所做的3个简单更改,请参见客户端脚本中的更改1和更改2,以及服务器端脚本中的更改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"];
            }
        }
    }
}
服务器端:

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"];
            }
        }
    }
}
错误消息:

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"];
            }
        }
    }
}
我使用谷歌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"}
问题:

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"];
            }
        }
    }
}

当使用
POST
时,任何人都知道为什么会发生这种情况。我只想做同样的事情,但需要使用
GET

问题是您将JSON作为查询字符串传递。它在POST中有效,但在GET中无效。 为了得到你需要的东西

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

在发出get请求时在查询字符串中添加数据查询字符串中确实有数据,不是吗?请参阅错误消息的第一行。您正在尝试以查询字符串形式传递json对象…服务器将无法理解使用该格式的请求您好,我已根据此更新了我的问题,因为即使在进行此更改后,错误仍在继续。请查看更新的问题。基于此,我收到以下错误消息:
GEThttp://intranet/webservice.asmx/GetCount?theDate=01/07/2013%2013:00:00%22 500(内部服务器错误)
xhr消息:{“消息”:“无效的JSON原语:07/2013 13:00:00\”。“,
让它工作了,它应该是
var date='”+strDate+'+strHours+:“+strmutes+':00”;
,谢谢