简体   繁体   中英

Generating Javascript in an ASPX page, and presenting it to Sharepoint

For reasons that I regret I've found myself in a situation where I'm trying to present data to a another page page via inserted JavaScript tags.

In essence what I'm doing is querying a database from within an aspx page, and writing the data out into javascript arrays. This aspx page is used as the src element in a script tag.

This only works in realease when the browser is IE.

The code might help to explain, [this page works as expected] http://server1/Website1/ <

<%@ Page ContentType="application/javascript" Language="C#" AutoEventWireup="true" CodeFile="GetDataFromDB.aspx.cs" Inherits="GetDataFromDB" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>

</body>
</html>

And in the code behind we have

public partial class GetDataFromDB: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        Response.ContentType = "application/javascript";

        StringBuilder output = new StringBuilder();

        output.Append(" var data= [");
        // Open database connection
        // Get some data
        // write it out into the output StringBuilder
        // close down connection
        output.Append("]; )";
        byte[] data = GetByteArray(output.ToString());

        Response.OutputStream.Write(data, 0, data.Length);
        Response.OutputStream.Flush(); 
        Response.End();
    }
}

Within the Sharepoint page I have included this script tag

<script language='javascript' type='application/javascript' src='http://server1/Website1/GetDataFromDB.aspx' ><\script>

Now this works fine in IE and the data is presented to the page, however in Firefox and Opera this appears to work OK but within the contents of the script file each character is interspersed with a ?, ie it begins:

v?a?r? d?a?t?a?=?[? .... etc

If I copy the contents of the generated page out to a text file and save it in the same location, eg http://server1/Website1/GetDataFromDB.js and use that as the source, everthing works.

Any one got any idea what I'm doing wrong?

Additional info, the tho pages are running from different hosts, and both are being severed out through IIS, I have full control of the server hosting http://server1/Website1/GetDataFromDB.aspx but no control at all over the one where the parent page is being server from.

Its my own fault... I based the function GetByteArray() on this SO post: How do I get a consistent byte representation of strings in C# without manually specifying an encoding?

However this was the cause of the issue, it should have been: https://stackoverflow.com/a/10379957

Works now...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM