我使用揭示模块模式和敲除来绑定窗体。当以这种形式输入数据(注册)时,需要将其发回MVC4网络方法。通过jQuery Ajax调用Web服务的参数调用
这里是jQuery代码
/*
Requires JQuery
Requires Knockout
*/
op.TestCall = function() {
// Private Area
var _tmpl = { load: "Loading", form: "RegisterForm"};
var
title = ko.observable(null)
template = ko.observable(_tmpl.load),
msg = ko.observable(),
postData = ko.observable(),
PostRegistration = function() {
console.log("Ajax Call Start");
var test = GetPostData();
$.ajax({
type: "POST",
url: obj.postURL, //Your URL here api/registration
data: GetPostData(),
dataType: "json",
traditional: true,
contentType: 'application/json; charset=utf-8'
}).done(Success).fail(Failure);
console.log("Ajax Call End");
},
GetPostData = function() {
var postData = JSON.stringify({
dummyText1: dummyText1(),
dummyText2: dummyText2(),
});
return postData;
}
return {
// Public Area
title: title,
template: template,
dummyText1: dummyText1,
dummyText2: dummyText2
};
}();
控制器代码是简单按照现在
// POST api/registration
public void Post(string data)
{
///TODO
}
当我试图,捕获数据(使用简单的console.log)并验证它在jslint.com,这是一个有效的Json。
我试图硬编码数据
数据: “{数据: '{\' 名称\ ':\' 尼尔\ '}'}”, 但我仍得为null,在我的网络方法。
添加标记
[System.Web.Script.Services.ScriptMethod(UseHttpGet = false, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
我Post方法在控制,但是仍没有成果而偶试过JSON.Stringify
data: JSON.stringify(data)
但我仍然能在这个网络的方法无效。
我无法找出解决方案。
类似的问题被发现@这个链接 http://forums.asp.net/t/1555940.aspx/1 甚至Passing parameter to WebMethod with jQuery Ajax 但并没有帮助我:(
我不能这样做,我需要使用JSON.stringify并获得所需的。我稍微更新了我的问题。谢谢你的回答。 :) – Shubh 2012-07-30 09:18:10