使用Javascript服务器端编码从Classic ASP返回JSONP [英] Return JSONP from Classic ASP with Javascript server side coding
问题描述
我正在从jquery调用经典的.asp页面以返回列表(使用JSONP).我想使用JSONP,因为通过Google翻译查看我的网站时,使用相同的来源政策会导致问题.我发现的所有解决方案的问题在于,它们假定asp页使用VBscript作为服务器端语言.我正在使用JavaScript作为ASP中的服务器端语言,以连接到数据库并返回结果.
I'm calling a classic .asp page from jquery to return a list (using JSONP). I want to use JSONP because of the same origin policy that causes problems when my website is viewed through google translate. The problem with all of the solutions I have found is that they assume the asp page is using VBscript as the server side language. I am using javascript as the server side language, in asp, to connect to a db and return results.
我尝试了几种仅返回500个服务器错误的方法.这是我的jquery调用:
I have tried a few different methods that have only returned 500 server errors. Here is my jquery call:
$.ajax({
dataType: 'jsonp',
url: 'website/page.asp',
success: function () {
alert(data);
},
});
这是我的page.asp:
And this my page.asp:
<%@ language="Javascript" %>
<script language="javascript" runat="server" src='json2.js'></script>
<script language="javascript" runat="server">
var jsonValue = eval('('hello world')');
Response.Write(jsonValue);
</script>
我要做的就是创建一个简单的hello world JSONP调用,以便我可以开始对其进行修改,以包含正在执行的查询中的数据到数据库.有人有这个的简单版本吗?这可能吗?
All I want to do is create a simple hello world JSONP call so that I can start to modify it to include data from a query I am doing to a database. Does anyone have a simple version of this? Is this possible?
推荐答案
您的示例没有任何意义.首先,它具有无效的语法("hello world"是一个字符串,但未引用).其次,您想在asp代码中编码JSON,但是使用eval是解码JSON的一种(错误的)方法.我认为您可以使用Crockford的json2.js对您的对象进行编码,如下所示:
Your example makes no sense. First, it has invalid syntax ("hello world" is a string but it is not quoted). Secondly, you want to encode JSON in your asp code, but using eval would be a (wrong) way to decode JSON. Here is what I think you could do, using Crockford's json2.js to encode your object, like this:
var sourceObj = { "testkey" : "test value", "otherkey" : 5 };
var jsonstr = JSON.stringify(sourceObj);
Response.Write("yourjsonpCallbackName (" + jsonstr + ");");
这篇关于使用Javascript服务器端编码从Classic ASP返回JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!