freeCodeCamp/guide/chinese/miscellaneous/how-jsonp-is-different-from.../index.md

22 lines
536 B
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: How Jsonp Is Different from JSON
localeTitle: Jsonp与JSON的区别
---
JSONP只是JSON包装在一个回调函数中。
JSONP对于发出跨域请求很有用出于安全原因这些请求通常被浏览器禁止。
```
// an example of JSON
{"weapon":"nunchucks","headband":"yellow"}
// an example of JSONP
myCallback({"weapon":"nunchucks","headband":"yellow"});
```
然后要访问存储在JSONP中的数据请定义回调函数
```
myCallback = function(data){
alert(data.weapon);
};
```