如何在JSON中使用代理 [英] how to use proxy with JSON

查看:156
本文介绍了如何在JSON中使用代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"dataFetch.php"的php页面,该页面位于一个Web服务器上.在另一个Web服务器上,我有一个JS文件,该文件向dataFetch发出JSON调用. dataFetch连接到数据库,检索数据并将其以JSON格式存储,并反馈给调用程序.在IE中,这可以正常工作.在其他浏览器中,并不是因为跨域限制.

I have a php page called 'dataFetch.php' which sits on one webserver. On another webserver, I have a JS file which issues JSON calls to dataFetch. dataFetch connects to a database, retrieves data and puts it in a JSON format which is fed back to the calling program. In IE, this works fine. In other browsers it does not because of the cross domain restriction.

要克服跨域限制,我对文件proxy.php进行了调用,然后对dataFetch进行了调用.现在我的问题是proxy.php从dataFetch检索文件,但是JS脚本文件不再将proxy.php的响应视为JSON格式,因此我无法处理它.有人可以帮我吗?

To get across the cross-domain restriction, I make a call to a file, proxy.php, which then makes the call to dataFetch. My problem now is that proxy.php retrieves the file from dataFetch but the JS script file no longer sees the response from proxy.php as a JSON format and so I can't process it. Can anybody help me out?

推荐答案

使用

Have a look at using JSONP instead, which solves the cross site difficulties you have had.

请解释代理的工作方式.代理应该非常简单,就像这样:

Please explain how the proxy works. A proxy should be very simple, something like this:

<?php
$url = $_GET['ur'];
echo file_get_contents($url);
?>

并以此方式使用:

http://www .example.com/proxy.php?url = http://www.someothersite.com/dataFetch.php

这篇关于如何在JSON中使用代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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