本文介绍了带PHP,MySQL,jQuery的跨域JSON通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

所以这是我当前的困境:我需要在一个页面中运行一些javascript,每30秒将一个JSON对象发回到驻留在不同域上的PHP Web服务.一旦这些数据进入跨域,PHP Web服务就会将其接收并推送到托管在该数据库中的数据库中.想法是拥有这种开源资源,这样任何想要在自己的网站上集成此应用程序的人都必须下载最少数量的文件.这是我目前对如何实现的理解的工作方式(请注意,当我指的是本地托管"时,指的是相关应用程序所在的服务器,而不是客户端):

1.在某人希望该应用程序运行的页面上有一个JSON对象.

2.每隔30秒,使用AJAX帖子将此JSON对象发送到本地托管的.php文件.

3.使本地托管的.php文件相应地格式化JSONP对象,并将其发送到跨域.php Web服务.

4.让Web服务将数据推送到本地(跨域)托管的MySQL数据库中.
5.让Web服务返回一个JSONP对象,该对象表示推送成功.

6.让本地托管的.php文件将成功标志返回到页面.

7.在页面本身上有一些javascript实现,显示上传符合计划.

话虽如此,这是我对此过程的疑问:

首先,这甚至是最好的方法吗?我听说JSON来回传递比xml更有效.

其次,假设这是一种适当的方法,那么有人可以为我提供一些参考资料,介绍如何通过php在各个域之间发送JSONP数据吗?

第三,如果这种方法存在任何明显的安全缺陷,它们是什么,我该如何克服它们? MySQL数据库和php Web服务将驻留的域是静态的,因此硬编码向其发送请求和接受响应的位置将不是问题.

任何有关如何进行此过程的建议,将不胜感激.

Hello all!

So here is my current predicament: I need to run some javascript within a page that, every 30 seconds, posts a JSON object back to a PHP web service that resides on a different domain. Once this data makes it to the cross-domain, it is taken by the PHP web service and pushed into a database hosted there. The idea is to have this open source so that anybody that wants to incorporate this application on their own site has to download a minimal amount of files. Here is how my current understanding of how this could be accomplished works (please note that when I refer to "locally hosted" I am referring to the server that the relevant application resides on, not the client):

1. Have a JSON object on the page where somebody wanted this application to run.

2. Every 30 seconds, use an AJAX post to send this JSON object to a locally hosted .php file.

3. Have the locally hosted .php file format the JSONP object accordingly and send it to the cross-domain .php web service.

4. Have the web service push the data into a locally (on the cross-domain) hosted MySQL database.

5. Have the web service return a JSONP object that signifies the push was successful.

6. Have the locally hosted .php file return the successful flag to the page.

7. Have some javascript implementation on the page itself that shows that the upload went according to plan.

All that being said, here are the questions I have about this process:

First and foremost, is this even the best approach? I have heard JSON is more efficient to pass back and forth than xml.

Second, assuming that this is a proper approach, can somebody point me to some reference material as to how to send JSONP data across domains via php?

Third, if there are any glaring security flaws with this approach, what are they and how do I overcome them? The domain that the MySQL database and php web service will reside on is static, so hard-coding where to send requests to and accept responses from would not be a problem.

Any advice whatsoever on how to approach this process would be greatly appreciated. Thank you in advance!

推荐答案



这篇关于带PHP,MySQL,jQuery的跨域JSON通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 12:02