有没有办法通过ajax调用返回javascript渲染的JSON

发布于 2024-12-18 03:16:34 字数 293 浏览 0 评论 0原文

我遇到的情况是,我必须运行一些 javascript 并从我的站点获取客户端站点的响应,并且由于跨域脚本安全性,我正在尝试考虑执行此操作的最佳方法。 我可以在客户端页面上放置一段代码。 我可以随意修改我自己的网站。

一种选择是通过 iframe 打开页面,让其网站上的代码片段运行以获取响应,并设置一个 cookie,我可以轮询该 cookie 以获取响应...哎呀...

我一直在考虑不同的方法这样做,我正在努力发挥创意。我是一个后端人员,有一点 javascript 经验,但不需要处理跨域的东西。如果有任何帮助,我将不胜感激。 谢谢,

I have a situation that I have to run some javascript and get a response on a clients site from my site, and I'm trying to think of the best way to do this because of cross domain scripting security.
I have access to placing a snippet of code on a client page.
I can modify my own site as much as I want.

One option is to open the page up via an iframe have the snippet on their site run get the response and set a cookie that I can poll for to get the response from...yikes...

I have been thinking of different ways to do this, and I'm trying to get creative. I'm a backend guy with a bit of javascript experience, but nothing having to deal with cross domain stuff. Please I would appreciate any help.
Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栀梦 2024-12-25 03:16:34

您可以在您的情况下使用 JSONP

JQuery 示例可以在此处找到

You could use JSONP in your situation.

Example with JQuery could be found here

失退 2024-12-25 03:16:34

使用您在客户端的代码片段动态创建 iframe - 然后您将位于客户端域沙箱中,并且能够向您的网站发送请求

with your snippet on the client side create the iframe dynamically - then you will be in a clients domain sandbox and will be able to send a request to your site

2024-12-25 03:16:34

也许您可以通过 ajax 调用来调用 Web 服务,并通过以下方式返回 JSON 字符串。

$.ajax({
            type: "POST",
            url: "JSON.asmx/Getdata",
            data: {},
            contentType: "text/javascript; charset=utf-8",
            ContentLength: 15000,
            dataType: "text",
            async: true,
            // timeout:10000,
            success: function (msg) {
                asmxdata(msg);
                return false;
            },
            error: function (xhr, ajaxOptions, thrownError, request, error) {
                alert('xrs.status = ' + xhr.status + '\n' +
                            'thrown error = ' + thrownError + '\n' +
                             'xhr.statusText = ' + xhr.statusText + '\n' +
                            'request = ' + request + '\n' +
                            'error = ' + error);
                return false;

            }
        });

然后,在 Web 服务调用成功(即此处的方法 asmxdata() )时,您可以通过以下方式将 JSON 字符串评估为 JSON 对象:

function asmxdata(data) {
var JObject = eval('(' + data + ');');
}

并为控件分配值

document.getElementById('lblfirstname').value = JObject.Table[i].FirstName;

如果有任何不清楚的地方请评论。

May be you can call a web service via ajax call and return JSON string by following way..

$.ajax({
            type: "POST",
            url: "JSON.asmx/Getdata",
            data: {},
            contentType: "text/javascript; charset=utf-8",
            ContentLength: 15000,
            dataType: "text",
            async: true,
            // timeout:10000,
            success: function (msg) {
                asmxdata(msg);
                return false;
            },
            error: function (xhr, ajaxOptions, thrownError, request, error) {
                alert('xrs.status = ' + xhr.status + '\n' +
                            'thrown error = ' + thrownError + '\n' +
                             'xhr.statusText = ' + xhr.statusText + '\n' +
                            'request = ' + request + '\n' +
                            'error = ' + error);
                return false;

            }
        });

Then on success of the Web service call (i.e method asmxdata() in here) you can evaluate the JSON string into a JSON object by following way:

function asmxdata(data) {
var JObject = eval('(' + data + ');');
}

and assign values to controls

document.getElementById('lblfirstname').value = JObject.Table[i].FirstName;

If anything is unclear please Comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文