我正在调用asmx webservice,它返回由knockout.js正确使用的json (msg.d)
。当我尝试将相同的json返回到asmx时,我收到错误消息。有什么明显的东西我不见了吗? ... msg.d
是一个格式良好的对象数组。
调用storeGroupCategories(msg.d);
会返回webservice错误...
{“消息”:“无效的JSON原语:Baby + Books。”
调用storeGroupCategories(msg);
会返回webservice错误...
{“Message”:“无效的JSON原语:d。”
的WebService
public class kbo_inexcludecategories : WebService
{
[WebMethod]
public List<Group> GetIncludeExcludeJson()
{
var Groups = new List<Group>();
ShopAssistGroupHandler.getInExCategories(Groups);
return Groups;
}
[WebMethod]
public GroupGuid StoreGroupCategories(List<InExCategory> inExCategories)
{
var inExString = JsonConvert.SerializeObject(inExCategories);
var returnGuid = DataHandler.SaveGroupJsonString(inExString);
return new GroupGuid(returnGuid);
}
}
相关的json ......
var _url = "kbo-inexcludecategories.asmx/";
var _method = "GetIncludeExcludeJson";
var _jsonData = "{}";
function storeGroupCategories(groupCategories) {
if(groupCategories != ""){
showProgressBar("Storing Group Categories");
getJsonData(_url, "StoreGroupCategories", groupCategories);
}
}
function getGroupMatrix() {
showProgressBar("Loading Group Categories");
getJsonData(_url, _method, _jsonData);
}
function getJsonData(url, method, jsonData) {
var myUrl = url + method;
$.ajax({
type: "POST",
url: myUrl,
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false, //blocks window close
success: onSuccess,
error: onError
});
}
function onSuccess(msg) {
// Hide the fake progress indicator graphic.
hideProgressBar("");
if(msg.d.hasOwnProperty("Guid")) {
saveGroupGuid(msg.d);
}
else {
storeGroupCategories(msg.d);
//showGroupAccordion(msg.d);
//OpenAdvancedDialog();
}
}
json样本......
"{\"groups\":[{\"__type\":\"group\",\"id\":1488,\"name\":\"Baby Books\",\"categories\":
[{\"__type\":\"groupcategory\",\"id\":152,\"name\":\"Activity Books\",\"value\":\"Included\"},
{\"__type\":\"groupcategory\",\"id\":167,\"name\":\"Bedtime and Dreams\",\"value\":\"Excluded\"}
答案 0 :(得分:0)
首先,我认为你需要像这样传递你的json:
storeGroupCategories(msg.d)
但是在这个函数中你还需要为帖子创建有效的json参数,如下所示:
getJsonData(_url, "StoreGroupCategories", "{ inExCategories: " + groupCategories + " }");
我还会将您的签名更改为以下内容,因此小组会匹配您传递的参数:
public GroupGuid StoreGroupCategories(List<InExCategory> groups)
如果您在网页方法中设置了一个断点,您将看到确切的结果,并检查它是否是您所期望的。