Thursday, December 16, 2010

CROSS DOMAIN POSTING ___ITS POSSIBLE

well.....finally i have worked out to post cross-domain which browser's security policies do not allow .

WHAT IS IT?
This will be used to (AJAX) posting from java-script to any web-service or web-page in any DOMAIN and  also get back the results from there.

HOW TO USE IT ?

simple use this function in your java script->>

 simple use this function in your java script->>

function jsonp(url, callback, name, query) 
{
    if (url.indexOf("?") > -1)
        url += "&jsonp="
    else
        url += "?jsonp="
    url += name + "&";
    if (query)      
        url += query+ "&";
    url += new Date().getTime().toString(); // prevent caching
    var script = document.createElement("script");
    script.setAttribute("src", url);
    script.setAttribute("type", "text/javascript");
    document.body.appendChild(script);
}


CALLING THIS FUNCTION from javascript :
jsonp("url of web page to be posted","", "function to be called back to receive result from the page","parameters  to be passed");

eg:
 jsonp('http://270.7.74.218/test/test.aspx', '', 'loadVideoPanel', "VID=" + VID + "&device=mobile" );    






ON WEB SERVICE :(like on http://270.7.74.218/test/test.aspx):

protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["jsonp"]))
            this.JsonPCallback();
    }

    public void JsonPCallback()
    {
       string hello ="hello";
       string Callback = Request.QueryString["jsonp"];
        Response.Write(Callback + "('" + hello + "');

    }
//callback here is your function that should be present on your JavaScript



BACK ON JAVASCRIPT:

loadVideoPanel(result)
{
alert(result);
}

which will alert you HELLO that i have passed from test.aspx page in another domain



this is it 


if u have any query plz let me know .......best O" lcuk





No comments:

Post a Comment