本文介绍了Ajax调用未达到服务器端方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好。 先谢谢。 当我点击按钮时它会在Ajax调用后调用JavaScript方法如果我在Firefox浏览器上调试它比调用服务器端方法setMandate(),但不是直接点击按钮,我不知道发生了什么? 请帮忙我急! :) < div > < 按钮 id = GetMandateBtn onclick = SetMandate() 已停用 > 获取授权< / button > < ; / div > function SetMandate(){ var clients = new Array (); var queryNo = 0 ; $( #DivMandateClients输入:选中)。each( function (){ clients.push($( this )。attr(' value')); }); if (clients == ) clients.push( 0 ); var contacts = new 数组(); $( #DivMandateClientsContact input:checked)。each( function (){ contacts.push($( this )。attr(' value')); var value = $( this )。val(); }); if (contacts == ) contacts.push( 0 ); var candidateStatus = new 数组(); $( #candidateStatus input:checked)。each( function (){ candidateStatus.push($( this )。attr(' value')); }); if (candidateStatus == ){ candidateStatus.push( 0 ); queryNo = 0 ; } var mandatoryRegion = new 阵列(); $( #MandateRegion input:checked)。each( function (){ mandatoryRegion.push($( this )。attr(' value')); }); if (mandatoryRegion == ){ mandatoryRegion.push( 0 ); queryNo = 0 ; } var missionCountry = new 阵列(); $( #MandateCountry input:checked)。each( function (){ mandCountry.push($( this )。attr(' value')); }); if (mandCountry == ){ missionCountry.push( 0 ); queryNo = 0 ; } var 研究人员= new 阵列(); $( #Researcher input:checked)。each( function (){ studies.push($( this )。attr(' value')); }); if (研究人员== ){ studies.push( 0 ); queryNo = 0 ; } if (StartDateTxt.value.trim()!= && EndDateTxt.value.trim()!= ){ $ .ajax({ url: / WebService / GetMandate, data: {'startDate':' + StartDateTxt.value + ','endDate':' + EndDateTxt.value + ','clients':' + clients + ','contacts':' + contacts + ','candidateStatus':' + candidateStatus + ,mandatoryRegion: + mandatoryRegion + ','mandCountry':' + mandCountry + ','研究人员':' +研究人员+ ','queryNo':' + queryNo + '}, dataType: json,类型: POST, dataFilter: function (data){ return data; },成功:功能(数据){ var 使命= $( #Mandate); mission.html( ); if (data!= null ){ if (data!= ) mand.append($( < input type ='checkbox'id ='chklstMandate-2'name = 'Mandate'value =' - 2'/>& nbsp;< label style ='display:inline;'id ='lblMandate-2'>选择全部< / label>< hr />)); $ .each(data, function (index,value){ mission.append($( < input type ='checkbox'id ='chklstMandate + data [index] .ResearcherId + 'name ='Mandate'value =' + data [index] .ResearcherId + '/>& nbsp;< label style ='display:inline;'id ='lblMandate + data [index] .ResearcherId + '> + data [index] .MandaName + < / label>< br />)); }); $( #chklstMandate-2)。click( function (event){ if ($( #chklstMandate-2)。is( :选中)) $( #mandate) .each( function (){ $( input [type = checkbox] [name ='Mandate'])。attr( 已选中, true ); }); else if ($( #chklstMandate-2)。not( :选中)) $( #Mandate)。each( function (){ $( input [type = checkbox] [name ='Mandate'])。attr( checked, false ); }); }); } },错误: function (XMLHttpRequest,textStatus,errorThrown){ alert( 加载命令错误 + errorThrown); } }); } }; public ActionResult GetMandate() { // InputStream包含您发送的JSON对象 字符串 jsonString = new StreamReader( this .Request.InputStream).ReadToEnd(); // 将其反序列化为字典 var dic = Newtonsoft.Json.JsonConvert.DeserializeObject< Dictionary< String,string>>(jsonString); var startDate = Convert.ToDateTime(dic [ startDate]); // DateTime.Parse(dic [startDate]); var endDate = DateTime.Parse(dic [ 结束日期]); string [] clients = dic [ 客户]。拆分(' ,'); string [] contacts = dic [ 联系人]。拆分(' ,'); string [] candidateStatus = dic [ candidateStatus]。分割(' ,'); string [] mandatoryRegion = dic [ mandatoryRegion]。分割(' ,'); string [] mandCountry = dic [ missionCountry]。分割(' ,'); string []研究人员= dic [ 研究人员]。分裂(' ,'); int queryNo = int .Parse(dic [ queryNo]); // bool isClientIdSelected = true; List< int> ; clientIds = new List< int>(); foreach ( var clnt in 客户) { 如果(clnt!= - 2&& clnt!= 0) clientIds.Add( int .Parse(clnt)); } List< int> contactIds = new List< int>(); foreach ( var conts in 联系方式) { if (conts!= - 2&& conts!= 0) contactIds.Add( int .Parse(conts)); } List< int> candidateStatusIds = new List< int>(); foreach ( var 状态 candidateStatus) { if (status!= - 2&& status!= 0) candidateStatusIds.Add( int .Parse(status)); } List< int> mandRegionIds = new List< int>(); foreach ( var region in mandatoryRegion) { if (region!= - 2&& region!= 0) mandatoryRegionIds.Add( int .Parse(region)); } List< int> mandCountryIds = new List< int>(); foreach ( var country in mandCountry) { if (country!= - 2&& country!= 0) missionCountryIds.Add( int .Parse(country)); } List< int> researcherIds = new List< int>(); foreach ( var 研究员 in 研究人员) { if (研究员!= - 2&& researcher!= 0) researcherIds.Add( int .Parse(researchar)); } if (queryNo == 0 ) { var mandaetFromDB =( from mc in dbContext.MandateCandidates join m in dbContext.Mandates on mc。 MandateId等于m.MandateId join mr 在mc.MandateId等于mr的 dbContext.MandateResearchers中。 MandateId 其中 m.StartDate > = startDate && m .EndDate < = endDate & &安培; clientIds.Contains(m.ClientId)&& contactIds.Contains(m.ContactId)&& candidateStatusIds.Contains(mc.CandidateStatusId ?? 0 ) select new {MandateId = m.MandateId,MandaName = m.Name,ResearcherId = mr.ResearcherId,ClientId = m.ClientId}。)ToList(); return Json(mandaetFromDB,JsonRequestBehavior.AllowGet); } else { var mandaetFromDB = ( from mc in dbContext.MandateCandidates join m in dbContext.Mandates on mc.MandateId equals m.MandateId join mr 在 dbContext.MandateResearchers上mc.MandateId等于mr.MandateId 其中 m.StartDate > = startDate && m.EndDate < = endDate && clientIds.Contains(m.ClientId) &安培;&安培; contactIds.Contains(m.ContactId)&& candidateStatusIds.Contains(mc.CandidateStatusId ?? 0 )&& mandatoryRegionIds.Contains(m.RegionId ?? 0 )&& mandCountryIds.Contains(m.MandateCountryId ?? 0 )&& researcherIds.Contains(mr.ResearcherId ?? 0 ) select new {MandateId = m.MandateId,MandaName = m.Name,ResearcherId = mr.ResearcherId,ClientId = m.ClientId}。)ToList(); return Json(mandaetFromDB,JsonRequestBehavior.AllowGet); } // 返回Json(mandaetFromDB,JsonRequestBehavior.AllowGet); } 解决方案 ( #DivMandateClients输入:选中)。each( function (){ clients.push( ( this )。attr(' value')); }); if (clients == ) clients.push( 0 ); var contacts = new 数组(); ( #DivMandateClientsContact input:checked)。每个( function (){ contacts.push( Hello allThanks in Advance.when i click on button it will call JavaScript method after that on Ajax call if i debug it on Firefox browser than it calls server side method "setMandate()" but not directly on button click, i don't know what is happening?please help me it urgent!!! :)<div> <button id="GetMandateBtn" onclick="SetMandate()" disabled>Get Mandate</button></div>function SetMandate() { var clients = new Array(); var queryNo = 0; $("#DivMandateClients input:checked").each(function () { clients.push($(this).attr('value')); }); if (clients == "") clients.push(0); var contacts = new Array(); $("#DivMandateClientsContact input:checked").each(function () { contacts.push($(this).attr('value')); var value = $(this).val(); }); if (contacts == "") contacts.push(0); var candidateStatus = new Array(); $("#candidateStatus input:checked").each(function () { candidateStatus.push($(this).attr('value')); }); if (candidateStatus == "") { candidateStatus.push(0); queryNo = 0; } var mandateRegion = new Array(); $("#MandateRegion input:checked").each(function () { mandateRegion.push($(this).attr('value')); }); if (mandateRegion == "") { mandateRegion.push(0); queryNo = 0; } var mandateCountry = new Array(); $("#MandateCountry input:checked").each(function () { mandateCountry.push($(this).attr('value')); }); if (mandateCountry == "") { mandateCountry.push(0); queryNo = 0; } var researchers = new Array(); $("#Researcher input:checked").each(function () { researchers.push($(this).attr('value')); }); if (researchers == "") { researchers.push(0); queryNo = 0; } if (StartDateTxt.value.trim() != "" && EndDateTxt.value.trim() != "") { $.ajax({ url: "/WebService/GetMandate", data: "{ 'startDate' : '" + StartDateTxt.value + "','endDate' : '" + EndDateTxt.value + "','clients' : '" + clients + "','contacts' : '" + contacts + "','candidateStatus' : '" + candidateStatus + "','mandateRegion' : '" + mandateRegion + "','mandateCountry' : '" + mandateCountry + "','researchers' : '" + researchers + "','queryNo' : '" + queryNo + "'}", dataType: "json", type: "POST", dataFilter: function (data) { return data; }, success: function (data) { var mandate = $("#Mandate"); mandate.html(""); if (data != null) { if (data != "") mandate.append($("<input type='checkbox' id='chklstMandate-2' name='Mandate' value='-2'/> <label style='display:inline;' id='lblMandate-2'>Select All</label><hr/>")); $.each(data, function (index, value) { mandate.append($("<input type='checkbox' id='chklstMandate" + data[index].ResearcherId + "' name='Mandate' value='" + data[index].ResearcherId + "'/> <label style='display:inline;' id='lblMandate" + data[index].ResearcherId + "'>" + data[index].MandaName + "</label><br />")); }); $("#chklstMandate-2").click(function (event) { if ($("#chklstMandate-2").is(":checked")) $("#Mandate").each(function () { $("input[type=checkbox][name='Mandate']").attr("checked", true); }); else if ($("#chklstMandate-2").not(":checked")) $("#Mandate").each(function () { $("input[type=checkbox][name='Mandate']").attr("checked", false); }); }); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert("Error on Loading Mandate" + errorThrown); } }); }};public ActionResult GetMandate() { // InputStream contains the JSON object you've sent String jsonString = new StreamReader(this.Request.InputStream).ReadToEnd(); // Deserialize it to a dictionary var dic = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<String, string>>(jsonString); var startDate = Convert.ToDateTime(dic["startDate"]);//DateTime.Parse(dic["startDate"]); var endDate = DateTime.Parse(dic["endDate"]); string[] clients = dic["clients"].Split(','); string[] contacts = dic["contacts"].Split(','); string[] candidateStatus = dic["candidateStatus"].Split(','); string[] mandateRegion = dic["mandateRegion"].Split(','); string[] mandateCountry = dic["mandateCountry"].Split(','); string[] researchers = dic["researchers"].Split(','); int queryNo = int.Parse(dic["queryNo"]); //bool isClientIdSelected = true; List<int> clientIds = new List<int>(); foreach (var clnt in clients) { if (clnt != "-2" && clnt != "0") clientIds.Add(int.Parse(clnt)); } List<int> contactIds = new List<int>(); foreach (var conts in contacts) { if (conts != "-2" && conts != "0") contactIds.Add(int.Parse(conts)); } List<int> candidateStatusIds = new List<int>(); foreach (var status in candidateStatus) { if (status != "-2" && status != "0") candidateStatusIds.Add(int.Parse(status)); } List<int> mandateRegionIds = new List<int>(); foreach (var region in mandateRegion) { if (region != "-2" && region != "0") mandateRegionIds.Add(int.Parse(region)); } List<int> mandateCountryIds = new List<int>(); foreach (var country in mandateCountry) { if (country != "-2" && country != "0") mandateCountryIds.Add(int.Parse(country)); } List<int> researcherIds = new List<int>(); foreach (var researcher in researchers) { if (researcher != "-2" && researcher != "0") researcherIds.Add(int.Parse(researcher)); } if (queryNo == 0) { var mandaetFromDB = (from mc in dbContext.MandateCandidates join m in dbContext.Mandates on mc.MandateId equals m.MandateId join mr in dbContext.MandateResearchers on mc.MandateId equals mr.MandateId where m.StartDate >= startDate && m.EndDate <= endDate && clientIds.Contains(m.ClientId) && contactIds.Contains(m.ContactId) && candidateStatusIds.Contains(mc.CandidateStatusId ?? 0) select new { MandateId = m.MandateId, MandaName = m.Name, ResearcherId = mr.ResearcherId, ClientId = m.ClientId }).ToList(); return Json(mandaetFromDB, JsonRequestBehavior.AllowGet); } else { var mandaetFromDB = (from mc in dbContext.MandateCandidates join m in dbContext.Mandates on mc.MandateId equals m.MandateId join mr in dbContext.MandateResearchers on mc.MandateId equals mr.MandateId where m.StartDate >= startDate && m.EndDate <= endDate && clientIds.Contains(m.ClientId) && contactIds.Contains(m.ContactId) && candidateStatusIds.Contains(mc.CandidateStatusId ?? 0) && mandateRegionIds.Contains(m.RegionId ?? 0) && mandateCountryIds.Contains(m.MandateCountryId ?? 0) && researcherIds.Contains(mr.ResearcherId ?? 0) select new { MandateId = m.MandateId, MandaName = m.Name, ResearcherId = mr.ResearcherId, ClientId = m.ClientId }).ToList(); return Json(mandaetFromDB, JsonRequestBehavior.AllowGet); } //return Json(mandaetFromDB, JsonRequestBehavior.AllowGet); } 解决方案 ("#DivMandateClients input:checked").each(function () { clients.push((this).attr('value')); }); if (clients == "") clients.push(0); var contacts = new Array();("#DivMandateClientsContact input:checked").each(function () { contacts.push( 这篇关于Ajax调用未达到服务器端方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-28 03:29