Return JSONP from Classic ASP with Javascript serv

2019-07-23 06:42发布

问题:

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.

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);
  },
});

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>

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?

回答1:

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 + ");");