tags:

views:

37

answers:

2

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&amp;tagmode=any&amp;format=json&amp;jsoncallback=?"
var testurl_b = "http://www.xxxxxx.com/myaccount/jsonserver.aspx?tags=cat&amp;tagmode=any&amp;format=json&amp;jsoncallback=?"

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

$.getJSON(testurl_a,function (data) {
        alert("JSON Data from testurl_a Result--->>>"+ data.title); //IS WORKING
 });
+2  A: 

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

<%=Request.QueryString["callback"]%>({
 "color": "blue",
            "animal": { "dog": "friendly" }
});
SLaks
Both the client AND the server need to implement JSONP for it to work properly.
mattbasta
I copied and paste your script in, it doesn't work man.
darwin
@darwin Try `<%=Request.QueryString["jsoncallback"]%>` instead.
mattbasta
A: 

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"&gt;&lt;/script&gt;
</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&amp;tagmode=any&amp;format=json&amp;jsoncallback=?"&gt;&lt;/script&gt;
</body>
</html>
mattbasta
@mattbasta, if the URL includes the string "callback=?" in the URL, the request is automatically treated as JSONP by jQuery. Of course he still does need to deal with this in his handler.
Ken Redler
I GOT IT. just the json formatsimply change to remove the ( and ) in the data format. Don't know why yet but i'll stick with this rule
darwin