Monday, March 26, 2012

This is killing me... need to display custom HTML from remote site to a calling site

Ok, I hope someone has done this or can help me here as I'm having trouble finding what I need.

What I want to do is allow users to include a javascript file in their website that will go out and pull data from my site / db and display it on their website. What will happen is they pass over some specific information that indicates what they want returned and then I'll serve it to their website where it displays in a naturally embedded format and looks like it's part of their own content.

An example of what I want to do is the same as the following:

 <div style="padding-left:10px; padding-right:10px;">
<asp:PlaceHolder ID="plcContent"runat="Server">
<script language="javascript" src="http://pics.10026.com/?src=http://www.datafeedfile.com/dff_jsthrow.php?affid=1001&script=search_index&style=12"></script>
</asp:PlaceHolder>
</div>

What the above line does is display contents (full HTML) from data feed file onto a webpage (shopping content), but doesn't actually render the full HTML to the webpage (if you right-click and view source). I want to do the same thing where someone calls a .ASPX or .ASMX page / service and I return (or serve) the HTML content. So, a call to my app may look like:

 <div style="padding-left:10px; padding-right:10px;">
<asp:PlaceHolder ID="plcContent"runat="Server">
<script language="javascript" src="http://pics.10026.com/?src=http://www.mysite.com/contentsource.aspx?affid=1001&type=X"></script>
</asp:PlaceHolder>
</div>
The contentsource.aspx page will then return (or display) a listing of products from my website on the calling website.  

Can that be done in ASP.NET 2.0? Any help much appreciated as I've been searching for a couple days (and many many hours of trial and error) with no luck. Thanks!

Hi,

Try to use IFRAME, see http://www.w3schools.com/tags/tag_iframe.asp


Add this to your page (and remove html from the ASPX that shouldn't be there, such as <body>, <head> etc.)

protected override void Render(HtmlTextWriter writer)
{
StringBuilder sb=new StringBuilder();
StringWriter sw =new StringWriter(sb);
HtmlTextWriter hw =new HtmlTextWriter(sw);
base.Render(hw);

Response.Clear();
Response.ContentType ="text/javascript";

string html =string.Format("window.document.write({0});",Enquote(sb.ToString()));
writer.Write(html);

}

/// FUNCTION Enquote Public Domain 2002 JSON.org
/// @.author JSON.org
/// @.version 0.1
/// Ported to C# by Are Bjolseth, teleplan.no
public static string Enquote(string s)
{
if (s ==null || s.Length == 0)
{
return"\"\"";
}
char c;
int i;
int len = s.Length;
StringBuilder sb =new StringBuilder(len + 4);
string t;

sb.Append('"');
for (i = 0; i < len; i += 1)
{
c = s[i];
if ((c =='\\') || (c =='"') || (c =='>'))
{
sb.Append('\\');
sb.Append(c);
}
else if (c =='\b')
sb.Append("\\b");
else if (c =='\t')
sb.Append("\\t");
else if (c =='\n')
sb.Append("\\n");
else if (c =='\f')
sb.Append("\\f");
else if (c =='\r')
sb.Append("\\r");
else
{
if (c <' ')
{
//t = "000" + Integer.toHexString(c);
string tmp =new string(c, 1);
t ="000" +int.Parse(tmp, System.Globalization.NumberStyles.HexNumber);
sb.Append("\\u" + t.Substring(t.Length - 4));
}
else
{
sb.Append(c);
}
}
}
sb.Append('"');
return sb.ToString();
}


Thanks, but that doesn't quit work for what I want. I just want users to be able to do it via a javascript call like in the example I provided. IFrames are kinda ugly, clunky, and not really something I would want to ask users to drop into their sites. I appreciate the reply though!


guntem,

That's exactly what I needed! Very very much appreciated as you've saved me much time and frustration.

David

0 comments:

Post a Comment