Jquery ajax 将数据传递到处理程序不起作用(Asp.net、C#、Jquery)
Jquery ajax with passing data into handler not working (Asp.net, C#, Jquery)
我已经使用 ajax 调用将一些表单数据传递给具有 json 格式的处理程序,但 ajax 无法正常工作。
我使用 $('form').serializeArray() 从表单数据传递数据,并使用 ajax 调用传递数据。在这种情况下,我传递了静态数据,但 ajax 调用仍然无效。
这是 jQuery 代码。
function SessionLogAutoSave() {
var sendInfo =
{
"ctl00$Header1$cbo_Practice": "ea2aae16-a7f9-4e75-82b5-d2c231c97cf6",
"ctl00$cph_MainContent$OFTAddRecord$hdnUrl": "",
"ctl00$cph_MainContent$hdnSessionId": "yyuicdueevmdeynp2q12mrb1",
"ctl00$cph_MainContent$hdnPracticeId": "870ee142-eb27-4d37-bba2-061fa5f801e3",
"ctl00$cph_MainContent$hdnRecordId": "",
"ctl00$cph_MainContent$hdnUserId": "d220da1a-d855-4f90-b8db-a84bc5ba8f51",
"ctl00$cph_MainContent$txt_Name": "",
"ctl00$cph_MainContent$txt_Address": "",
"ctl00$cph_MainContent$txt_ContactNumber": "",
"ctl00$cph_MainContent$cbo_ComplaintBy": "1",
"ctl00$cph_MainContent$cbo_ComplaintReceivedBy": "83beebf4-e6ea-49b3-a64f-ed20f755afdd",
"ctl00$cph_MainContent$cbo_PersonHandlingComplaint": "83beebf4-e6ea-49b3-a64f-ed20f755afdd",
"ctl00$cph_MainContent$cbo_Type": "1",
"ctl00$cph_MainContent$txt_ComplaintReceivedOn": "",
"ctl00$cph_MainContent$txt_ComplaintReceivedOn$dateInput": "",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_ClientState": "",
"ctl00$cph_MainContent$txt_DateFollowup": "",
"ctl00$cph_MainContent$txt_DateFollowup$dateInput": "",
"ctl00_cph_MainContent_txt_DateFollowup_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateFollowup_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateFollowup_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateFollowup_ClientState": "",
"ctl00$cph_MainContent$txt_DetailsComplaint": "",
"ctl00$cph_MainContent$txt_Followup": "",
"ctl00$cph_MainContent$txt_DateActionTaken": "",
"ctl00$cph_MainContent$txt_DateActionTaken$dateInput": "",
"ctl00_cph_MainContent_txt_DateActionTaken_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateActionTaken_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateActionTaken_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateActionTaken_ClientState": "",
"ctl00$cph_MainContent$txt_DateOutcome": "",
"ctl00$cph_MainContent$txt_DateOutcome$dateInput": "",
"ctl00_cph_MainContent_txt_DateOutcome_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateOutcome_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateOutcome_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateOutcome_ClientState": "",
"ctl00$cph_MainContent$txt_ActionTaken": "",
"ctl00$cph_MainContent$txt_Outcome": "",
"ctl00$cph_MainContent$rbtnDiscussed": "0",
"ctl00$cph_MainContent$cbo_Status": "1",
"ctl00$cph_MainContent$hid_Id": "",
"ctl00$cph_MainContent$hdnAttachments": "",
"ctl00$cph_MainContent$hdnFileNames": "",
"ctl00$cph_MainContent$cbo_Months": "4",
"ctl00$cph_MainContent$cbo_Years": "2021"
};
jQuery.ajax({
url: 'Handlers/SaveSessionLogAutoSave.ashx',
data: 'FormData=' + JSON.stringify(sendInfo) + '',
contentType: 'application/json;utf-8',
dataType: 'json',
type: 'GET',
cache: false,
success: function (data) {
// you can make a tiny notification here
return;
},
error: function (responseText, textStatus, XMLHttpRequest) {
return;
}
});
}
我已经使用处理程序来检索数据。我的处理程序代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DentalCompliance.Handlers
{
/// <summary>
/// Summary description for SaveSessionLogAutoSave
/// </summary>
public class SaveSessionLogAutoSave : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string method = context.Request["FormData"];
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
//}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
我在互联网上搜索了 1 天后仍未找到任何解决方案。
将您的代码更改为:
$.ajax({
url: 'Handlers/SaveSessionLogAutoSave.ashx',
data: 'FormData=' + JSON.stringify(sendInfo) + '',
dataType: 'json',
type: 'POST',
cache: false,
success: function (data) {
// you can make a tiny notification here
return;
},
error: function (responseText, textStatus, XMLHttpRequest) {
return;
}
});
更改了两件事:1. 请求类型从 GET 到 POST 因为当我们使用 GET,您发送的数据附加到 URL,您发送的数据过多,超出了应用程序允许的 URL 长度。
2. 删除了 contentType 因为你没有发送 JSON.
我已经使用 ajax 调用将一些表单数据传递给具有 json 格式的处理程序,但 ajax 无法正常工作。 我使用 $('form').serializeArray() 从表单数据传递数据,并使用 ajax 调用传递数据。在这种情况下,我传递了静态数据,但 ajax 调用仍然无效。
这是 jQuery 代码。
function SessionLogAutoSave() {
var sendInfo =
{
"ctl00$Header1$cbo_Practice": "ea2aae16-a7f9-4e75-82b5-d2c231c97cf6",
"ctl00$cph_MainContent$OFTAddRecord$hdnUrl": "",
"ctl00$cph_MainContent$hdnSessionId": "yyuicdueevmdeynp2q12mrb1",
"ctl00$cph_MainContent$hdnPracticeId": "870ee142-eb27-4d37-bba2-061fa5f801e3",
"ctl00$cph_MainContent$hdnRecordId": "",
"ctl00$cph_MainContent$hdnUserId": "d220da1a-d855-4f90-b8db-a84bc5ba8f51",
"ctl00$cph_MainContent$txt_Name": "",
"ctl00$cph_MainContent$txt_Address": "",
"ctl00$cph_MainContent$txt_ContactNumber": "",
"ctl00$cph_MainContent$cbo_ComplaintBy": "1",
"ctl00$cph_MainContent$cbo_ComplaintReceivedBy": "83beebf4-e6ea-49b3-a64f-ed20f755afdd",
"ctl00$cph_MainContent$cbo_PersonHandlingComplaint": "83beebf4-e6ea-49b3-a64f-ed20f755afdd",
"ctl00$cph_MainContent$cbo_Type": "1",
"ctl00$cph_MainContent$txt_ComplaintReceivedOn": "",
"ctl00$cph_MainContent$txt_ComplaintReceivedOn$dateInput": "",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_ComplaintReceivedOn_ClientState": "",
"ctl00$cph_MainContent$txt_DateFollowup": "",
"ctl00$cph_MainContent$txt_DateFollowup$dateInput": "",
"ctl00_cph_MainContent_txt_DateFollowup_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateFollowup_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateFollowup_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateFollowup_ClientState": "",
"ctl00$cph_MainContent$txt_DetailsComplaint": "",
"ctl00$cph_MainContent$txt_Followup": "",
"ctl00$cph_MainContent$txt_DateActionTaken": "",
"ctl00$cph_MainContent$txt_DateActionTaken$dateInput": "",
"ctl00_cph_MainContent_txt_DateActionTaken_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateActionTaken_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateActionTaken_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateActionTaken_ClientState": "",
"ctl00$cph_MainContent$txt_DateOutcome": "",
"ctl00$cph_MainContent$txt_DateOutcome$dateInput": "",
"ctl00_cph_MainContent_txt_DateOutcome_calendar_SD": "[]",
"ctl00_cph_MainContent_txt_DateOutcome_calendar_AD": "[[1980,1,1],[2099,12,30],[2021,4,22]]",
"ctl00_cph_MainContent_txt_DateOutcome_dateInput_ClientState": "{\"enabled\":true,\"emptyMessage\":\"\",\"validationText\":\"\",\"valueAsString\":\"\",\"minDateStr\":\"1980-01-01-00-00-00\",\"maxDateStr\":\"2099-12-31-00-00-00\",\"lastSetTextBoxValue\":\"\"}",
"ctl00_cph_MainContent_txt_DateOutcome_ClientState": "",
"ctl00$cph_MainContent$txt_ActionTaken": "",
"ctl00$cph_MainContent$txt_Outcome": "",
"ctl00$cph_MainContent$rbtnDiscussed": "0",
"ctl00$cph_MainContent$cbo_Status": "1",
"ctl00$cph_MainContent$hid_Id": "",
"ctl00$cph_MainContent$hdnAttachments": "",
"ctl00$cph_MainContent$hdnFileNames": "",
"ctl00$cph_MainContent$cbo_Months": "4",
"ctl00$cph_MainContent$cbo_Years": "2021"
};
jQuery.ajax({
url: 'Handlers/SaveSessionLogAutoSave.ashx',
data: 'FormData=' + JSON.stringify(sendInfo) + '',
contentType: 'application/json;utf-8',
dataType: 'json',
type: 'GET',
cache: false,
success: function (data) {
// you can make a tiny notification here
return;
},
error: function (responseText, textStatus, XMLHttpRequest) {
return;
}
});
}
我已经使用处理程序来检索数据。我的处理程序代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace DentalCompliance.Handlers
{
/// <summary>
/// Summary description for SaveSessionLogAutoSave
/// </summary>
public class SaveSessionLogAutoSave : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string method = context.Request["FormData"];
context.Response.ContentType = "text/plain";
context.Response.Write("Hello World");
//}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
我在互联网上搜索了 1 天后仍未找到任何解决方案。
将您的代码更改为:
$.ajax({
url: 'Handlers/SaveSessionLogAutoSave.ashx',
data: 'FormData=' + JSON.stringify(sendInfo) + '',
dataType: 'json',
type: 'POST',
cache: false,
success: function (data) {
// you can make a tiny notification here
return;
},
error: function (responseText, textStatus, XMLHttpRequest) {
return;
}
});
更改了两件事:1. 请求类型从 GET 到 POST 因为当我们使用 GET,您发送的数据附加到 URL,您发送的数据过多,超出了应用程序允许的 URL 长度。 2. 删除了 contentType 因为你没有发送 JSON.