0

我想在我的代码json中将响应读取为名称和值对。JQuery这是我从 dotnet 代码返回的示例 JSON 响应:

const string format = "\"HasCases\": \"{0}\""; 
StringBuilder json = new StringBuilder(128); 
json.Append("{"); 
json.AppendFormat(format, JSONString("true"));
json.Append("}"); 
Response.Clear(); 
Response.AppendHeader("Content-type", "application/json; charset=utf-8");                                                                               Response.Write(json.ToString()); 
Response.End();

要获取 Json 值,是否有必要使用响应代码?在我的 Json 页面中,我能够将输出作为 HasCases:true。

这是我的 JQuery 代码

<span id="testSpan" runat="server">inactive</span>
<script type="text/javascript">
inactive 
$.ajax({ 
        type: 'POST',
        url: "~/Pages/UserCaselistnonEmptyAjax.aspx", 
        dataType: "json", 
        success: function (response){
        $('#testSpan').innerHTML = response.HasCases; 
       }, 
     error: function (e1, e2, e3) {
  $('#testSpan').innerHTML = 'Error';
      }
   });
 </Script>

当我在调试表单时,firebug我的控件不会。"$('#testSpan').innerHTML = response.HasCases; "它是从循环中出来的。

4

2 回答 2

1

jQuery 对象不实现.innerHTML. 改用.html()

$('#testSpan').html(response.HasCases);
于 2012-12-26T07:18:51.337 回答
0

我正在使用返回我的 json

return (new JavaScriptSerializer().Serialize(request)); 

在我的 c# 代码中。我正在调用在页面加载事件中返回此值的函数。输出是这个

{"registration_ids":["1","2"],"data":{"message":"Your message","tickerText":"Your ticker","contentTitle":"Your content"}}

但我无法用 jquery ajax 读取这个返回的 json fomrat 我的 ajax 在下面

function as()
  {

                $.ajax({
                      type:"get",
                      url:"mydjson.aspx",
                      contentType: 'application/json;charset=utf-8', 
                      dataType: {'json':'datas'}, 
                      //data: JSON.stringify(request),//{ get_param: 'value' },
                      success:function (msg) {

                                         $("#table").html("<h1>" + msg+ "</h1>").fadeIn("slow");

                                    },
                                      //  function (data, status)

                     error: function (xhr, textStatus, error) 
                            {
                               var errorMessage = error || xhr.statusText;

                                $("#table").html("<h3>" + errorMessage + "</h3>").fadeIn("slow");
                            }
                   });
        return false;
   //   });

}

我收到错误“Ivalid json”以及页面“mydjson.aspx”的内容。请帮帮我。

于 2013-01-19T06:02:42.420 回答