2

我创建了一个 HTML 文件,该文件将尝试将 json 值传递给我的 asp 页面,但我的 asp 出现错误。我的 HTML 工作正常并传递值 {'a':'a','b':'b'},但 asp 页面无法使用该值。

  1. 这是我的 HTML 代码并显示 JSON 值 {'a':'a','b':'b'}:

      <html>
      <head>
      <title></title>
    
      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
      <script type="text/javascript"> 
    
        $(document).ready(function() {
        $('#btnAdd').click(function() { 
    
    
            var json_obj = "{'" + $('#t1').val() + "' : '" + $('#p1').val() + "','" + $('#t2').val() + "' : '" + $('#p2').val() + "'}"; 
    
    
            $.ajax({ 
                type: 'POST', 
                url: 'http://localhost/Base_Data/InsertItem.aspx', 
                contentType: 'application/json; charset=utf-8', 
                data: json_obj, 
                dataType: 'json', 
                success: function(msg) { 
                    alert('Success!'); 
                }, 
                error: function(msg) { 
                    alert('Error!'); 
                } 
            }); 
        }); 
      }); 
      </script> 
      </head>
      <body>
    
    <div> 
    Type: 1: <input type="text" id="t1" /> 
    Property 1: <input type="text" id="p1" /> 
    
    Type 2: <input type="text" id="t2" /> 
    Property 2: <input type="text" id="p2" /> 
    <input type="button" id="btnAdd" value="Add object!" /> 
    
    </div> 
    
    </body>
    </html>
    
  2. 这是我背后的 ASP 页面代码:

    public class Test
    {
    public Test(string json)
    {
    
        var serializer = new JavaScriptSerializer();
        var result = serializer.DeserializeObject(json);
    
        var first = int.Parse(result["t1"]);
        var second = int.Parse(result["t2"]); 
     }
    
      public string first { get; set; }
      public string second { get; set; }
     }
    

任何答复将不胜感激。提前致谢!

4

2 回答 2

0

When passing data to the $.ajax method, the data parameter is generally a javascript object with key/value pairs and those key/value pairs are put into a query string as in foo=bar&person=ted. Putting a JSON string in there and then trying to parse that JSON in your server is the hard way to send a couple values to your server and it's a lot more work to parse it on the server too.

Using the built in data capabilities of the $.ajax method will put the data into query string format and your server can automatically parse that out too - all without writing any new code on client or server.

From the jQuery docs:

data

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

于 2012-09-04T08:55:51.433 回答
0

我会避免手动生成纯 JSON,因为它总是会导致问题,尝试使用JSON.stringify 之类的东西,这将使您的数据构造更容易,因为您可以使用正确的 JS 对象,例如

var jsonObj = { t1: $('#t1').val(), p1: $('#p1').val(), t2: $('#t2').val(), p2: $('#p2').val() };

$.ajax({  
    type: 'POST',  
    url: 'http://localhost/Base_Data/InsertItem.aspx',  
    contentType: 'application/json; charset=utf-8',  
    data: JSON.stringify(jsonObj),  
    dataType: 'json',  
    success: function(msg) {  
        alert('Success!');  
    },  
    error: function(msg) {  
        alert('Error!');  
    }  
}); 

然后在服务器端将您的 json 反序列化为一个dynamic对象并取回数据:

public Test(string json)  
{  
    var serializer = new JavaScriptSerializer();  
    var result = serializer.Deserialize<dynamic>(json);  

    var first = int.Parse(result["t1"]);  
    var second = int.Parse(result["t2"]);   
}

更新

根据您的评论,您将无法使用dynamic对象,您应该能够将您的 JSON 反序列化为Dictionary例如

var result = serializer.Deserialize<Dictionary<string, string>>(json);
于 2012-09-04T09:32:12.773 回答