SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation getJSON using ASP.NET

    i am unable to get the value from cs page .


    my code is below here :

    js page :

    var jsonp_url = "http://192.168.1.29/Webwidget/Test.aspx?callback=?";
    $.getJSON(jsonp_url, function (data) {
    alert('data');
    $('#example-widget-container').html("This data comes from another server1: " + data.html);
    });

    cs page :-



    protected void Page_Load(object sender, EventArgs e)
    {

    Response.ContentType = "application/json;charset=utf-8";
    String strReturn = String.Empty;
    strReturn = Request.QueryString["callback"].ToString() + "({\"html\":" + "Hello World!" + " })";
    Response.ContentType = "text/javascript";
    Response.Write(strReturn.ToString());


    }//Page_Load



    please need your help.

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,878
    Mentioned
    47 Post(s)
    Tagged
    0 Thread(s)
    It is tricky to debug without actually having the code infront of me, but a few things I pick up on:

    The response type should not be text/javascript, it should be application/json, like you have set above, but then change before you write to the stream.

    And the most important thing, your output is not valid json. As the above will output: ?({"html":Hello world!})
    So what you want to do is remove the callback query string and brackets so:
    strReturn = "{\"html\":'\"Hello World!\"}" which will then output this: {"html":"Hello World!"}

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,635
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    You should really use something like the WebAPI to handle jsonp requests, your code won't correctly wrap the json to be jsonp.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •