错误。从 ASP.NET 页面获取 JSONP
我正在使用 Jquery-JSONP 从 ASP.NET 页面进行跨域获取
我的 ASP.NET 页面如下所示;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("functionName({'test_param':12345});");
}
}
它作为 test.aspx 托管在我的服务器上,
我现在从本地主机使用 JQuery 并尝试从 test.aspx 中获取数据,如下所示;
$.ajax({
dataType: 'jsonp',
url: 'http://abc.com/GTalk/test.aspx?callback=?',
success: function () {
alert("Success");
},
error: function (x, y, z) {
alert("error" + x.responseText);
}
});
我收到错误 x.responseText = "undefined"
错误 z 是 Error: jQuery171008073005825281143_1328259709467
我无法弄清楚我犯了什么错误。我是新手。
谢谢。
I am doing a cross-domain fetch from a ASP.NET page using Jquery-JSONP
My ASP.NET page looks like this;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("functionName({'test_param':12345});");
}
}
This is hosted on my server as test.aspx
I am now using JQuery from localhost and trying to fetch the data from test.aspx like this;
$.ajax({
dataType: 'jsonp',
url: 'http://abc.com/GTalk/test.aspx?callback=?',
success: function () {
alert("Success");
},
error: function (x, y, z) {
alert("error" + x.responseText);
}
});
I am getting an error x.responseText = "undefined"
The error, z, is Error: jQuery171008073005825281143_1328259709467
I am not able to figure out what mistake I am doing. I am a newbie.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将通过 GET 参数收到的函数名称打印为 callback 参数(在这种情况下为jQuery171008073005825281143_1328259709467),而不是服务器端的“functionName”。
您无法对名称进行硬编码,每个请求的名称都不同)
Instead of "functionName" on serverside you must print the function-name you receive via the GET-params as callback-parameter(jQuery171008073005825281143_1328259709467 in that case).
You can't hardcode the name, it differs on every request)
我并不是自称是这里的专家,只是一个感兴趣的旁观者。
从上一篇文章来看,测试工具似乎是错误的,即您的页面。困难在于 - 如何处理动态回调名称?我进行了一些研究,找到了一个使用 asmx webservices 和 httpmodules 来获得你想要的东西的示例。
访问远程 ASP.Net Web 服务
如果我是您,会将测试页重做为 Web 服务(抱歉,我不太了解 WCF)并使用您自己的方法连接模块。在
JsonHttpModule
中,看起来Write
方法正在做有趣的工作,即将响应写入包装在动态回调方法签名中,并从中获取查询字符串(JQuery 将把它放在哪里)。不幸的是,看起来您还需要做一些工作才能让它发挥作用。可能有更好更快的方法,但我发现的就是这些。
I'm not claiming to be an expert here just an interested bystander.
From the previous post it looks as if the test harness is wrong i.e. your page. The difficulty being - how do you cope with the dynamic callback name? I've dug around a bit and I found an example that uses asmx webservices and httpmodules to get what you want.
Accessing Remote ASP.Net webservices
If I were you and would redo the test page as a webservice (sorry not very WCF I know) and wire in th module with your own method. Within the
JsonHttpModule
it looks like theWrite
method is doing the interesting work i.e. it is that that is wrapping the response write in a dynamic callback method signature that it is picking up from the query string (where JQuery will put it).Unfortunately it looks like you've got a bit of work ahead of you to get it working. There might be better quicker ways but that all I found.