如何处理跨域iframe文件上传json响应? [英] How to handle cross domain iframe file upload json response?

查看:220
本文介绍了如何处理跨域iframe文件上传json响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立档案上传API。

I'm building an files upload API.

基本上,用户必须使用他/她的api_key +签名将文件发布到我的Web服务。然后我的web服务回复一个JSON响应。我想知道这个过程如何可以异步运行?

Basically, the user will have to POST the files with his/her api_key + signature to my web service. Then my web service replies back with a JSON response. I'm wondering how can this process work asynchronously?

假设用户在将目标设置为iframe的表单中POST请求。 JSON响应将在内容类型设置为text / html的iframe上发送回用户。它被设置为text / html而不是application / json,因为我想避免在浏览器的JSON响应附近插入一个pre标签。无论如何,如果iframe和父窗口有不同的域,用户如何读取JSON响应?

Assuming that the user POST the request in a form setting the target to an iframe. The JSON response will be sent back to the user on his/her iframe with content type set as "text/html". It is set as "text/html" instead of "application/json" because I want to avoid having a "pre" tag injected by the browser around the JSON response. Anyway, how does the user read that JSON response if the iframe and the parent window have different domain? There is going to be a cross domain policy issue.

在这种情况下,动态创建脚本标记和JSONP将无法正常工作,因为我需要POST才能上传。 JSONP只适用于GET请求。

Dynamically create "script" tag plus JSONP won't work in this case because I need to POST in order to upload. JSONP only works with GET requests.

推荐答案

由于同源策略,浏览器不允许JavaScript在主框架读/来自其他网域的iframe中的内容。在这种情况下,用户将必须使用easyXDM或创建自己的代理 - 通过代理在这里我的意思是用户将不得不在他们的后端写一些代码,可以与您的API沟通,使发布请求将直接到您的服务器,并且get响应将从自己的代理获得。

Because of Same Origin Policy, browsers wont allow JavaScript in the main frame reading/accessing whatever content in iframe from another domain. In this case, the users will have to use easyXDM or create their own proxy -- by proxy here i mean users will have to write some code on their backend that can communicate with your API such that a post request will go directly to your server, and a get response will get from their own proxy.

这篇关于如何处理跨域iframe文件上传json响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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