2012-02-08 42 views
1

我有一个JSON对象,我需要从服务器传递到客户端以将数据保存在数据库中。将JSON传递给Web方法并出错。

我可以使用成功地将它传递:

var str2 = JSON.stringify({ 
    o: oJSON 
}); 

$.ajax({ 
    type: "POST", 
    url: "WebForm1.aspx/Test", 
    data: str2, 
    processData: false, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (msg) {}, 
    error: function (e) { 
     alert(e); 
    } 
}); 

<System.Web.Services.WebMethodAttribute(),    
System.Web.Script.Services.ScriptMethodAttribute()> _ 
Public Shared Function Test(ByVal o As RootObject) As String 
    Dim strName as string = textboxName.text 
End Function 

我必须保存两个不同的数据集的关断。在保存json对象之前必须保存第一组。它正在将文本框值保存到数据库中。第二组是将json存储到数据库。

我想设置sName为等于文本框的值和正在以下错误:

Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class

是否有另一种方式向JSON对象传递给客户端,我可以访问该文本框页面上的值?
或者我将不得不包括Name以及其他值,我需要在JSON对象?

+0

你说你想去“服务器 - >页面 - >数据库”。这听起来不对,你不想去“页面 - >服务器 - >数据库”?如果这是您想通过Ajax使用JSON来完成的工作,那么您需要一个具有2个值的方法(例如:beforeValue和afterValue)。既然你想同时发送之前/之后的值,你需要将before值存储在一个不会改变的地方(比如隐藏字段,JS varible等)。 – Zachary 2012-02-08 23:27:05

回答