JSON - 跨域请求 [英] JSON - Cross domain request

查看:66
本文介绍了JSON - 跨域请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望制作一个小型网络应用程序,它将从客户端浏览器返回

网站的状态。我尝试使用AJAX并在本地工作了很好的b $ b但是对于访问该页面的远程用户不起作用,我发生了一个跨域请求遇到安全问题。


我一直在阅读使用JSON的跨域请求可以完成

,但是无法找到任何关于如何使用的示例

它或如何从检索网页获得返回代码。


我想从客户端浏览器发出这些请求,它不会

作为我的Web服务器的代理请求。关于如何制作

这项工作的任何想法?请提供一个工作示例(如果可用)...

谢谢

I am looking to make a small web app that will return the status of a
website from the client browser. I tried this with AJAX and it worked
great locally, but did not work for remote users accessing the page, I
ran into the security problem with making a cross domain request.

I have been reading that with JSON a cross domain request can be
accomplished, but have not been able to find any examples on how to use
it or how to get the return code from retrieving a web page.

I want to make these requests from the client browser, and it will not
work as a proxy request from my web server. Any ideas on how to make
this work? Please provide a working example if available...
Thanks

推荐答案



tr****@gmail.com 写道:


tr****@gmail.com wrote:
我想要从客户端浏览器发出这些请求,它不会作为我的Web服务器的代理请求工作。关于如何使这个工作的任何想法?
I want to make these requests from the client browser, and it will not
work as a proxy request from my web server. Any ideas on how to make
this work?




图像对象或img元素对象可以与onload一起使用和

onerror handler eg

var img = new Image();

img.onerror = function(evt){

//句柄错误case

};

img.onload = function(evt){

//句柄负载

};

//现在尝试在要检查的服务器上加载测试图像,例如

img.src =''http://example.com/test.gif ?time =''+ new Date()。getTime();

这要求您可以将图像放在要检查的服务器上。

你不要没有任何状态代码,你必须忍受onerror

或onload被解雇。


另一种可能的方法是放置或生成上面的脚本

服务器例如

< script type =" text / javascript"

src =" http:// example。 COM / status.js">< /脚本> <无线电通信/>
至少允许您从与HTML文档来自的服务器不同的源服务器加载资源,例如:你可以用


var serverStatus = false;
带内联脚本的
然后在服务器上设置status.js

serverStatus = true;


我不知道JSON如何帮助解决相同的原始政策,

它只是一个数据交换格式无法帮助您制作

请求服务器,由于安全限制,您无法访问这些服务器。


-


Martin Honnen
http://JavaScript.FAQTs.com/


你的例子很有意义,但不完全是我想要的。

基本上我想要一个测试页面给出根据客户体验,网页状态

。所以我想要状态网站如
http://www.yahoo.com http://www.google.com 获取返回代码。如果您访问该页面,您将获得200返回代码,但如果我访问

页面,我可能因代理问题而获得不同的返回代码,或者

其他一些本地网络问题。我希望能够比较来自不同地点的用户

体验。


我在雅虎网站上找到了这个,但我找不到任何关于如何实现



使用JSON和动态< script>标签代替XML和XMLHttpRequest。

您可以直接在< script>内部对您的Web服务请求进行

来解决浏览器安全问题。标签。如果您使用的Yahoo!

Web服务可以输出JSON(使用output = json和

callback = function参数),那么您从web

服务在页面加载时被评估为JavaScript对象。


我怎样才能使用上面的内容来完成我想要做的事情?谢谢!

Your examples make sense, but not exactly what I am looking for.
Basically I want a test page that will give the status of a webpage
based on the client experience. So I would want to status sites like
http://www.yahoo.com or http://www.google.com for the return code. If
you visit the page you would get a 200 return code, but if I visit the
page I might get a different return code because of proxy issues, or
some other local network issue. I want to be able to compare the user
experience from different locations.

I found this on the Yahoo site, but I could not find any example on how
to implement:

Use JSON and dynamic <script> tags instead of XML and XMLHttpRequest.
You can get around the browser security problem altogether by making
your web services request directly inside a <script> tag. If the Yahoo!
Web Service you''re using can output JSON (using the output=json and
callback=function parameters), the data you get back from the web
service is evaluated as a JavaScript object when the page is loaded.

How could I use the above to accomplish what I am trying to do?? Thanks!




tr ** **@gmail.com 写道:



tr****@gmail.com wrote:

使用JSON和动态<脚本>标签代替XML和XMLHttpRequest。
您可以直接在< script>内部直接制作您的Web服务请求来解决浏览器安全问题。标签。如果你使用的Yahoo!
Web服务可以输出JSON(使用output = json和
callback = function参数),那么你将从web
服务中获取的数据被评估加载页面时作为JavaScript对象。

如何使用上面的内容来完成我想要做的事情?
Use JSON and dynamic <script> tags instead of XML and XMLHttpRequest.
You can get around the browser security problem altogether by making
your web services request directly inside a <script> tag. If the Yahoo!
Web Service you''re using can output JSON (using the output=json and
callback=function parameters), the data you get back from the web
service is evaluated as a JavaScript object when the page is loaded.

How could I use the above to accomplish what I am trying to do??




他们建议使用脚本元素从该服务器加载脚本

,我已经建议过,只是他们似乎打算动态创建
脚本元素(例如

document.createElement(''script'')

)然后让加载的脚本调用
$ b中定义的回调函数$ b你的页面。我的示例建议稍微简单一点,只是全局

变量用于数据交换,他们希望你使用回调

函数,脚本可以调用并传入JSON格式的数据。那个

当然是可能的,只有它没有帮助你从服务器获得任何状态

服务器上没有可用的特殊脚本

你可以加载。所以他们做的是提供你或其他任何人的网络服务

可以使用脚本元素调用,而你想得到任何没有脚本可用的任何

服务器的状态。


-


Martin Honnen
http://JavaScript.FAQTs.com/


这篇关于JSON - 跨域请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆