我想在aspx页面的客户端创建对象。我想为这些JavaScript类添加功能以使生活更轻松。使用JQUERY ajax函数发送JSON对象到ASP.NET Web服务
其实我可以得到并使用从服务返回的对象(派生自服务器端类)。当我想的jQuery AJAX方法来发送来自客户端的对象,我不能这样做:)
这是我的JavaScript类:
function ClassAndMark(_mark, _lesson){
this.Lesson = _lesson;
this.Mark = _mark;
}
function Student(_name, _surname, _classAndMark){
this.Name = _name;
this.SurName = _surname;
this.ClassAndMark = _classAndMark;
}
这是学生类的方法来调用Web服务:
JSClass.prototype.fSaveToDB(){
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/WS/SaveObject.asmx/fSaveToDB"),
data: ????????????,
// This might be: JSON.stringify(this) ?
// Web service method has a parameter, name is _obj
// if i don't send data with parameter, i'm getting this error:
// Invalid web service call, missing value for parameter: '_obj'
//
// Should i send it like that:
// data: "{_obj:" + JSON.stringify(this) + "}"
//
// I tried to wrap this with parameter like that:
// data: JSON.stringify("{_obj:" + this + "}")
//
// But i got this error:
// Cannot convert object of type 'System.String' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'
dataType: "json"
});
}
创建JavaScript对象,并调用它的方法来发送它向Web服务:
其实我不知道应该是什么的类和方法定义S于服务器端,但我认为:
class ClassAndMark{
public string Lesson ;
public string Mark ;
}
class Student{
public string Name ;
public string SurName ;
public ClassAndMark classAndMark ;
}
Web服务是低于但我又不能得到应该的,而不是什么???? :
[WebMethod()]
public Student fSaveToDB(???? _obj)
{
// How can i convert input parameter/parameters
// of method in the server side object?
// SQL operations
// srting QInsert = "INSERT INTO tableName (......) VALUES (.....)";
// ...
// ..
// .
return new Student{
Name = ???, // deserialize _obj and pass its Name value
SurName = ???, // deserialize _obj and pass its SurName value
classAndMark = ???, // deserialize _obj and pass its classAndMark value
};
}
这不工作“数据:JSON.stringify(myObj)”,因为Web服务方法有参数名称,它给出了错误,如“你没有用这个参数调用我”... – uzay95 2010-04-15 14:17:42
你试过包装你的对象,就像我在最后一行写的那样? 像这样:data:JSON.stringify({paramName:myObj}) – mamoo 2010-04-15 14:28:20
我试过并得到这个错误:无法将类型'System.String'的对象转换为类型'System.Collections.Generic.IDictionary'2 [System.String ,System.Object]' 你有什么想法吗?并感谢您的帮助... – uzay95 2010-04-15 14:45:39