(ASP.NET JAVASCRIPT 和 C#) 隐藏 javascript 调用 C# 方法的结果
我试图隐藏从我的 aspx 文件中的 javascript 代码调用的 C# 方法的结果。当我查看页面源时,我想返回值,目前为“HI_MOM!”,不可见。
我的 ASPX:
<html>
<head>
<title>Hide Me</title>
</head>
<body><div id="center"><div id="fig">
<script type="text/javascript">
var url = <%="'"+magic()+"'"%>;
document.write(url);
</script>
</div></div></body>
</html>
我的 C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string magic()
{
return "HI_MOM!";
}
}
运行后的源代码:
<html>
<head>
<title>Hide Me</title>
</head>
<body><div id="center"><div id="fig">
<script type="text/javascript">
var url = HI_MOM!;
document.write(url.toString);
</script>
</div></div></body>
</html>
基本上,我想做到这一点,以便当我查看页面的源代码时,出现“var url = HI_MOM!;”行对用户不可见或以某种方式被屏蔽。
编辑:
答案:(感谢@Shadow Wizard为我指明了正确的方向)
在TestTest.aspx中:
<script type="text/javascript" src="./jquery.js"></script>
。 。 。
jQuery.ajax({
type: 'POST',
url: 'TestTest.aspx/magic',
cache: false,
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: 'text',
success: function (msg) {
var result = eval('(' + msg + ')');
result = eval('('+result["d"]+')');
//do something with your string =]
}
});
在 TestTest.aspx.cs 中:
[WebMethod]
public static string magic()
{
return "HI_MOM!";
}
I am trying to hide the result of a C# method which is called from my javascript code in my aspx file. When I view the page source I want to return value, presently 'HI_MOM!', to not be visible.
My ASPX:
<html>
<head>
<title>Hide Me</title>
</head>
<body><div id="center"><div id="fig">
<script type="text/javascript">
var url = <%="'"+magic()+"'"%>;
document.write(url);
</script>
</div></div></body>
</html>
My C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class TestTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string magic()
{
return "HI_MOM!";
}
}
Source Code after Running:
<html>
<head>
<title>Hide Me</title>
</head>
<body><div id="center"><div id="fig">
<script type="text/javascript">
var url = HI_MOM!;
document.write(url.toString);
</script>
</div></div></body>
</html>
Basically, I want to make it so that when I view the source code for the page the line "var url = HI_MOM!;" is not visible to the user or is masked in some way.
EDIT:
ANSWER: (with thanks to @Shadow Wizard for pointing me in the right direction)
In TestTest.aspx:
<script type="text/javascript" src="./jquery.js"></script>
.
.
.
jQuery.ajax({
type: 'POST',
url: 'TestTest.aspx/magic',
cache: false,
data: '{}',
contentType: 'application/json; charset=utf-8',
dataType: 'text',
success: function (msg) {
var result = eval('(' + msg + ')');
result = eval('('+result["d"]+')');
//do something with your string =]
}
});
In TestTest.aspx.cs:
[WebMethod]
public static string magic()
{
return "HI_MOM!";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为此,您必须使用 AJAX。
使用 jQuery 是一种选择,您可以将
magic
转换为页面方法,然后 使用它。You will have to use AJAX for this.
Using jQuery is one option, with this you can turn
magic
to be Page Method then consume it.用户可以查看您想要作为 JavaScript 运行的所有内容。您可以让它看起来不太明显,甚至可以对其进行加密,但它仍然存在于浏览器中。
真正隐藏这一点的唯一方法是在服务器端完成所有操作。
Everything you want to run as javascript will be viewable to the user. You could make it look less obvious what it is doing or even go as far as encrypting it, but it's still all there in the browser.
The only way to truly hide this would be to do it all server side.