将数据传递到处理程序的 Jquery ajax 不起作用(Asp.net、C#、Jquery)
Posted
技术标签:
【中文标题】将数据传递到处理程序的 Jquery ajax 不起作用(Asp.net、C#、Jquery)【英文标题】:Jquery ajax with passing data into handler not working (Asp.net, C#, Jquery) 【发布时间】:2021-07-16 13:59:05 【问题描述】:我使用 ajax 调用将一些表单数据以 json 格式传递给处理程序,但 ajax 无法正常工作。 我已经使用 $('form').serializeArray() 从表单 Data 传递数据并使用 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 天的搜索,我无法找到任何解决方案。
【问题讨论】:
【参考方案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。
【讨论】:
以上是关于将数据传递到处理程序的 Jquery ajax 不起作用(Asp.net、C#、Jquery)的主要内容,如果未能解决你的问题,请参考以下文章
简单的烧瓶应用服务器使用 ajax 和 jquery 传递数据
将数据从 jquery ajax 请求传递到 wcf 服务失败反序列化?
为 jquery datatables ajax 调用设置错误处理程序