0

I need to make a cross domain ajax call to a server I own but the problem is that the request must come from the client not the server so proxies wont work for me. Our server will be behind a vpn so it won't be able to reach the internet but the client will be able to so we wanted to do a call home from the client to our metrics server to validate a product key.

My remote domain has a php script that simply writes either a 0, 1, or 2. I need my javascript to read this value in and react to it.

I want to do something simple like this but clearly it won't work. Any suggestions?

            $.ajax({
                url: callHomeUrl,
                type: 'GET',
                success: function(res) {
                    document.write($(res.responseText).text());
                }
            });
4

1 回答 1

0

您可以使用 JSONP 样式实现来访问您的服务器,该服务器可以在所有浏览器上运行,无需 CORS!

例子 -

var script=document.createElement('script'):
   script.type='text/javascript'; script.src='path/to/the/file';
document.getElementsByTagName('head')[0].appendChild(script);

请注意,服务器中的文件应输出 javascript 函数以及任何相关数据。

于 2013-03-14T17:32:25.683 回答