gpt4 book ai didi

javascript - 无法从静态方法运行 Javascript

转载 作者:行者123 更新时间:2023-11-28 08:40:47 32 4
gpt4 key购买 nike

我正在尝试从静态方法运行一个简单的警报脚本(打算在将来运行函数),但它没有向我显示警报(如果我在页面加载事件中执行此操作,则效果很好):

 Page page = HttpContext.Current.CurrentHandler as Page;

if (page != null)
{
string myScript = "<script type=\"text/javascript\" language=\"Javascript\">";
myScript += "alert('hi');";
myScript += "</script>";
page.ClientScript.RegisterClientScriptBlock(page.GetType(), "alert", myScript);
}

如果我调试 ClientScript 确实运行,有人可以帮忙吗?

编辑

这是我想在 ajax webmethod 的 成功事件之后运行的示例脚本:

<script type="text/javascript">
var OrderReference = '<%= Id %>';
var EID = <%= EId %>;
var Comment = '';
var SubDomain = 'track';

if (location.protocol.toLowerCase() == 'https:')
wgProtocol = 'https';
else
wgProtocol = 'http';

Uri = wgProtocol + '://' + SubDomain + '.test.com/tr.html' + '?&eid=' + EID
+ '&orderreference=' + OrderReference ;

document.write('<sc' + 'ript language="JavaScript" type="text/javascript" src="' + Uri + '"></sc' + 'ript>');
</script>

<noscript>
<img src='http://test.com/transaction.html?ver=1&eventid=<%= EId %>&wgorderreference=<%= Id %>&' alt='' />
</noscript>

已经尝试将上述脚本作为字符串返回,并在 ajax 方法中尝试执行以下操作,但在 Firebug 中收到 400 bad request:

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = result;
document.body.appendChild(script);

编辑2:

我创建了这个类:

[Serializable]
public class PixelGenericResults
{
public int Id{ get; set; }
public string Script { get; set; }
}

在我的静态方法中,我这样做:

 var g = new GenericResults();
g.Id = 2;
g.Script = AffiliateTracking.getScript(g.Id); //This gets me the above script

HttpContext.Current.Response.ContentType = "application/json";
strReturn = new JavaScriptSerializer().Serialize(g);

在 aspx 页面中我成功了:

 success: function (result) {
if (result.hasOwnProperty("d")) { result = result.d; }
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = result.Script;
document.getElementsByTagName('body')[0].appendChild(script);
}

但是运行它时,我在 firebug “NetworkError: 404 Not Found - http://localhost:5822/undefined”中收到此错误

最佳答案

正如评论中所述,您无法直接与使用 AJAX 调用呈现的页面进行交互,因此您必须在客户端做一些工作。

这是一个基于您向我们展示的代码的示例:

c#:

//import this namespace:
using System.Web.Script.Serialization;

// declare a class to contain all the info you will need client-side
[Serializable]
public class GenericResult
{
public int OrderReference { get; set; }
public string URL {get; set;}
public string EID { get; set; }
public string Comment { get; set; }
// etc. etc.
}

//In your static method, instance and populate such class:
var r = new GenericResult();
r.OrderReference = 34;
r.URL = "http://www.google.com";
r.EID = "someString";
r.Comment = "this will work like a charm";
this.Response.ContentType = "application/json";
this.Response.Write(new JavaScriptSerializer().Serialize(r));

JS:

//In your AJAX 'onsuccess' callback, you can use the object very easily, eg:
function callback(r){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = r.URL;
document.getElementsByTagName('body')[0].appendChild(script);
}

我尚未对此进行测试,因此它可能包含任何拼写错误,但这应该足以让您调整代码。

编辑:

 success: function (result) {
//what is the next line for?!?!?!
//if (result.hasOwnProperty("d")) { result = result.d; }

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = result.Script;
document.getElementsByTagName('body')[0].appendChild(script);
}

关于javascript - 无法从静态方法运行 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499046/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com