Reputation: 125
I want to read json
response as name and value pairs in my JQuery
code. Here is my sample JSON response that I return from my dotnet code:
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();
To get Json value ,is it necessary to use Response code?In my Json page , I am able to get the output as HasCases : true.
Here is my JQuery code
<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>
When I am debugging form firebug
My control does not going to "$('#testSpan').innerHTML = response.HasCases; "
.It is coming out from the loop.
Upvotes: 0
Views: 549
Reputation: 1
I am returning my json using
return (new JavaScriptSerializer().Serialize(request));
in my c# code.and I am calling the function returning this value at page load event. and the output is this
{"registration_ids":["1","2"],"data":{"message":"Your message","tickerText":"Your ticker","contentTitle":"Your content"}}
but I am unable to read this returned json fomrat with jquery ajax my ajax is below
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;
// });
}
I am getting the error "Ivalid json" plus the content of page" mydjson.aspx".help me for this.
Upvotes: 0
Reputation: 298166
jQuery objects don't implement .innerHTML
. Use .html()
instead:
$('#testSpan').html(response.HasCases);
Upvotes: 1