我已经看到了超过 30 个关于发送 json 数组的问答,但是我有一个不同且奇怪的问题......
我认为它通过 jqgrid 向用户表示一些数据。用户操作其中的一些行并调用操作方法来显示基于该数据的报告。我的问题是我必须通过路由值发送该数组,并且无法摆脱错误请求 - 无效 URL
错误...
我的观点对应部分:
// ... some correct codes before
var bpics = [],
for (var i = 0; i < myData.length; i++) {
var pid = "#chk" + myData[i].ID,
sid = "#ddl" + myData[i].ID,
pic = {
PersonnelName: myData[i].PersonnelName,
FatherName: myData[i].FatherName,
BirthDate: myData[i].BirthDate,
Education: myData[i].Education,
UniversityName: myData[i].UniversityName,
PostName: $(pid).val(),
Phone: myData[i].Phone,
NeedSettle: $(sid).val()
};
bpics.push(pic);
}
window.open("../RawPrint/GetBurseLetterReport/" + JSON.stringify(bpics)
+ "/" + $("#txtDestManName").val()
+ "/" + $("#ddlUniversity").val()
+ "/" + $("#lblCourseTitle").text());
// ... some other correct codes after ...
事实上,我正在使用 StimulSoft Reports
,该组件指示开发人员通过路由值发送变量。因此,我正在努力通过路由值发送 JSON 字符串(因为我必须......!)。
我做过的一些作品:
在网络配置中,我添加了以下标签。然而,当我使用 $.ajax
发送 json 数组时,它就可以工作......:
<httpRuntime targetFramework="4.5" requestPathInvalidCharacters="" requestValidationMode="2.0" />
和
<pages validateRequest="false">
有什么帮助、提示、技巧......?!
最佳答案
JSON.stringify(bpics) 结果是这样的字符串
[{PersonnelName: '值',$FatherName: '值',出生日期: '值',...},{PersonnelName: '值',$FatherName: '值',出生日期: '值',. ..},....]
所以实际的url格式会是这样的
window.open("../RawPrint/GetBurseLetterReport/[{PersonnelName: 'value',$FatherName: 'value',出生日期: 'value',...},{PersonnelName: 'value',$FatherName : '值',出生日期: '值',...},....]/destmanname/university/coursetitle")
在 url 中,您有特殊字符“:”,这会导致无效的 url(400 错误请求)错误
您应该将末尾的数组值作为参数传递,例如
http://yoururl?bpics=JSON.stringify(bpics);
您可以在参数中传递任何文本,因此即使您在参数中传递特殊字符,也不会收到 400 错误。
在 Controller 内部写这样的东西
public ActionResult GetBurseLetterReport(YourClass[] bpics)
{
....
}
YourClass 包含所有属性,例如 PersonalName、FatherName ..
公共(public)类YourClass
{
公共(public)字符串个人名称{get;set;}
....
....
}
关于javascript - 如何通过路由值将 JSON 数组发送到操作方法 (MVC 4),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17728346/