Please help me with the following situation:
there is the page p1.aspx with only one button:
<button id="btn1" onclick="btnclick();">Button</button>
<script type="text/javascript">
$('#btn1').click(function () {
$.getJSON("http://localhost/p2.aspx", function (data) {
$.each(data, function (i, field) {
alert(field);
});
});
});
</script>
Above is how I want to get the JSON text via javascript.
Web application http://localhost/p2.aspx is redirected to http://localhost/p3.aspx inside. And the page http://localhost/p3.aspx again is redirected back to
http://localhost/p2.aspx?code=1.
code=1
is the value I want read in my javascript code. But it's not works.
In p2.aspx I generate JSON data as following
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(jsonString);
Response.End();
After this I can not read json data via javascript. But if I just put the http://localhost/p2.aspx via web browser then it get json data on the page.
/p2.aspxjsonString?jsonstringis { flag: "1" } just the simple value to try get json objectcrossDomainand/or adding?callback=?doesn't magically make it work. You need to make sure the server supports it. If you add?callback=?, you're requesting JSONP. This means you should return a JavaScript file (application/javascript) using the callback param as a function call: like$_GET['callback'] . '(' . json_encode($data) . ');'(I use PHP, adapt to your language). Or use CORS, by setting theAccess-Control-Allow-Originheader on the server (enable-cors.org)