简体   繁体   English

在ASPX页面中生成Javascript,并将其呈现给Sharepoint

[英]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. 由于令人遗憾的原因,我发现自己正试图通过插入的JavaScript标签将数据呈现给另一个页面。

In essence what I'm doing is querying a database from within an aspx page, and writing the data out into javascript arrays. 本质上,我正在做的是从aspx页面中查询数据库,并将数据写到javascript数组中。 This aspx page is used as the src element in a script tag. 此aspx页在脚本标记中用作src元素。

This only works in realease when the browser is IE. 仅当浏览器是IE时,这才有效。

The code might help to explain, [this page works as expected] http://server1/Website1/ < 该代码可能有助于解释,[此页面按预期工作] 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 在Sharepoint页面中,我包含了此脚本标签

<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: 现在,这在IE中可以正常工作,并且数据显示在页面上,但是在Firefox和Opera中,这似乎可以正常工作,但是在脚本文件的内容中,每个字符都带有一个?,即开始:

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. 如果我将生成的页面的内容复制到一个文本文件中,并将其保存在相同的位置,例如http://server1/Website1/GetDataFromDB.js并将其用作源,则一切正常。

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. 其他信息,这些页面从不同的主机运行,并且都通过IIS进行了隔离,我对托管http://server1/Website1/GetDataFromDB.aspx的服务器拥有完全控制权,但对那父页面是服务器的来源。

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? 这是我自己的错...我基于此SO post函数GetByteArray(): 如何在C#中获得字符串的一致字节表示而无需手动指定编码?

However this was the cause of the issue, it should have been: https://stackoverflow.com/a/10379957 但这是问题的原因,应该是: https : //stackoverflow.com/a/10379957

Works now... 现在可以使用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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