开发者

How do I out put the JSON format for other website?

How do I out put the JSON format for other website? I wrote the code below. I've tried to retrieve the information from the JSON server but it didn't return anything back.

JSON SERVER

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
      //Response.ContentType = "application/json; charset=utf-8";
      Response.ContentType = "application/x-javascript; charset=utf-8";
      Response.AddHeader("Cache-Control", "no-cache");
      //Response.Expires = 0;
     // Response.AddHeader("content-disposition", "attachment;filename=" + Guid.NewGuid().ToString("N") + ".js");
    }
</script>
({
 'color': 'blue',
            'animal': { 'dog': 'friendly' }
})

calling JSON SERVER

  var testurl_a = "http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?"
var testurl_b = "http://www.xxxxxx.com/myaccount/jsonserver.aspx?tags=cat&tagmode=any&format=json&jsoncallback=?"

$.getJSON(testurl_b, function (data) {
    alert("JSON Data from testurl_b"); //IS NOT WORKING
});

$.getJSON(testurl_a,function (data) {
        a开发者_StackOverflow社区lert("JSON Data from testurl_a Result--->>>"+ data.title); //IS WORKING
 });


Your server-side JSON generator needs to implement the JSONP protocol, like this:

<%=Request.QueryString["callback"]%>({
 "color": "blue",
            "animal": { "dog": "friendly" }
});


The reason that you're not getting a response back is because of cross-domain issues. To get around this, use JSONp instead.

<%@ Page Language="C#" %>
<%@ Import Namespace="System" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
      //Response.ContentType = "application/json; charset=utf-8";
      Response.ContentType = "application/x-javascript; charset=utf-8";
      Response.AddHeader("Cache-Control", "no-cache");
      //Response.Expires = 0;
     // Response.AddHeader("content-disposition", "attachment;filename=" + Guid.NewGuid().ToString("N") + ".js");
    }
</script>
callback({
 'color': 'blue',
            'animal': { 'dog': 'friendly' }
})

And your client:

<!DOCTYPE html>
<html>
<title>Test</title>
<head>
  <script  type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script type="text/javascript">
<!--
function callback(data) {
    // Your callback code goes here.
}
-->
</script>
<script type="text/javascript" src="http://www.JSONServer.com/myaccount/jsonserver.aspx?tags=cat&tagmode=any&format=json&jsoncallback=?"></script>
</body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜