WP Rest API:在禁用永久链接的情况下使用 JSONP 和路由?

WP Rest API: Using JSONP and Routes with permalinks disabled?

我正在使用 WORDPRESS REST API 和 Wordpress 4.8 版。我们禁用了永久链接(出于安全原因),因此我正在访问 posts 对象,如下所示:

https://wordpress-dosstx.c9users.io/?rest_route=/wp/v2/posts

这在 Chrome 和某些版本的 IE 中工作正常,但在 FireFox 和其他几个不同浏览器版本中收到 CORS 请求失败 消息。

我想我会尝试设置使用 JSONP 的请求来绕过此限制。但是,由于我们关闭了永久链接,我不确定如何编写路线。根据 WP API docs,API 本身支持 JSONP 响应,我会这样写:

<script>
function receiveData( data ) {
  // Do something with the data here.
  // For demonstration purposes, we'll simply log it.
  console.log( data );
}
</script>
<script src="https://wordpress-dosstx.c9users.io/?rest_route=/wp/v2/posts?_jsonp=receiveData"></script>

但是,由于我没有使用永久链接,我该如何在脚本标签中编写脚本 URL? 文档说要像这样编写索引路由所以:

https://wordpress.org/wp-json/wp/v2/ would become https://wordpress.org/?rest_route=/wp/v2

我尝试将 jsonP 回调函数追加到 post 对象,结果显示 404 未找到。知道我应该将 JSONP 语法添加到路由的正确方法是什么吗?我的以下尝试没有奏效。

<script src="https://wordpress-dosstx.c9users.io/?rest_route=/wp/v2/posts?_jsonp=receiveData"></script>

JSFIDDLE 可用于测试:JSFIDDLE

你好像打错了,多了一个?在 url 查询字符串中。

替换:

src="https://wordpress-dosstx.c9users.io/?rest_route=/wp/v2/posts?_jsonp=receiveData"

与:

src="https://wordpress-dosstx.c9users.io/?rest_route=/wp/v2/posts&_jsonp=receiveData"

确保使用 & 分隔查询字符串中的多个参数。参见例如this informative Wikipedia article 了解更多信息。