确实JSONP需要服务器的修改? [英] Does JSONP require server modifications?

查看:1765
本文介绍了确实JSONP需要服务器的修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,JSONP是一种技术来绕过同源策略。你基本上是指你的JSON服务的服务器端点的脚本标记,因为脚本标记可免除SO政策。

I understand that jsonp is a technique to get around the same origin policy. You basically refer to your json serving server endpoint in a script tag, because script tags are exempt from the SO policy.

我的问题是:假设一台服务器有一个端点提供了JSON,有没有在服务器上,使客户端使用JSONP必要的任何修改

My question is: Assuming a server has an endpoint that serves up json, are there any modifications necessary on the server to make use of jsonp in the client?

我觉得没什么,但要确保......

I think no, but want to be sure....

推荐答案

是的,JSONP略有不同,当它呈现,让您的服​​务器需要支持它。

Yes, JSONP is slightly different when it renders, so your server needs to support it.

JSON看起来是这样的:

JSON looks like this:

{ "name": "value" }

而JSONP是这样的:

Whereas JSONP looks like this:

functionName({ "name": "value" });

如果无论你正在使用它你盖的支持,但它是不一样的支持只是JSON。当服务器收到一个请求,例如: http://example.com/json?callback=functionName ,上面的是你应该渲染,因为它的外观在该页面是这样的:

If whatever you're using supports it you're covered, but it's not the same as supporting just JSON. When the server gets a request, for example: http://example.com/json?callback=functionName, the above is what you should render, because how it looks in the page is this:

<script type="text/javascript" src="http://example.com/json?callback=functionName"></script>


这意味着运行需要退回,作为说明的东西,这是有效的:


This means something that runs needs to be returned, as an illustration, this is valid:

<script type="text/javascript">
  functionName({ "name": "value" });
</script>

如果你的服务器的没有的支持JSONP这实际上是这样的:

If your server didn't support JSONP it would effectively be this:

<script type="text/javascript">
  { "name": "value" }
</script>

......,你会得到语法错误,因为这不是有效的JavaScript。

...and you'll get syntax errors, since that's not valid JavaScript.

这篇关于确实JSONP需要服务器的修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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