freeCodeCamp/guide/chinese/miscellaneous/learn-about-jsonp/index.md

28 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
title: Learn About Jsonp
localeTitle: 了解Jsonp
---
## JSONP
JSONP代表“带填充的JSON”。假设您想要将AJAX请求发送到其他域。好吧你不能像往常那样使用XMLHttpRequest做到这一点但你可以用脚本标签来做到这一点如[StackOverflow所示](https://stackoverflow.com/questions/2067472/what-is-jsonp-all-about)
```javascript
script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://www.someWebApiServer.com/some-data';
```
但这很难看现在我们必须从脚本标签中获取JSON的元素粗略。幸运的是JSONP的创建者正在考虑未来所以我们不是像上面那样设置脚本而是这样做
```javascript
script.src = 'http://www.someWebApiServer.com/some-data?callback=my_callback';
```
这会在数据加载后触发自动回调,从而创建一个包含所需数据的函数。
### 更多信息:
* [Wikipidea / JSONP](https://en.wikipedia.org/wiki/JSONP)
* [JSONP和JQuery](https://learn.jquery.com/ajax/working-with-jsonp)
* [更多JSONP与JQuery](http://api.jquery.com/jquery.getjson/#jsonp)
* [Ajax和JSONP](http://stackoverflow.com/questions/5943630/basic-example-of-using-ajax-with-jsonp)