我正在我的.aspx页面上执行ajax调用
传递一些包含'/' , '&' , '-' , '.'
等字符的值,因此使用encodeURIComponent
$('#divDialog').html('<p>Loading Please wait...</p>').dialog({ title: 'Processing Information', modal: true, resizable: false, draggable: false }).load('ajaxExecute.aspx?Fn=CAO2',
{
'EmploymentType': encodeURIComponent($('#cmbEmploymentType').val()), 'NatureIncome': encodeURIComponent($('#cmbIncomeNature').val())
},
function (response, status, xhr) {
$('#divDialog').dialog('close');
// Some Code
}
});
我想在c#
中获取这些值if (Request.Form["EmploymentType"] != "" && Request.Form["EmploymentType"] != null) string sEmpType = Convert.ToString(Request.Form["EmploymentType"]);
QuickWatch
在Convert.ToString(Request.Form["EmploymentType"])
作为Car%2FTruck%2FBoat%2FPlane%20Dealer
我试过 HttpUtility.UrlEncode(Convert.ToString(Request.Form["EmploymentType"]))
但是结果相同
我怎样才能得到字符串Car/Truck/Boat/Plane Dealer
因为它在变量中?
答案 0 :(得分:1)
您正在使用Request.Form,这仅适用于提交,而不适用于ajax请求。看看C#WebMethods他们会将请求数据转换为C#对象。继承人example project
在ajaxExecute.aspx.cs中:
[WebMethod]
public static void DoFoo(String EmploymentType, String NatureIncome){
string sEmpType = EmploymentType;
}
使用Javascript:
$(...).load('ajaxExecute.aspx/DoFoo',
{
'EmploymentType' : $('#cmbEmploymentType').val(),
'NatureIncome': $('#cmbIncomeNature').val()
}
// rest of arguments
}