。現在,如果我需要通過另一個域的AJAX使用該API,則需要發送一個回調參數以及我的請求。在Restler中是否支持這個功能(我還沒有找到任何真實的文檔)?Luracast Restler回調參數爲我使用<a href="http://luracast.com/products/restler/" rel="noreferrer">Restler</a>實現一個簡單的REST API跨域AJAX
回答
對於任何人來到這個網頁從谷歌上搜索,我提出一個問題,在github上,並得到了從作者的大力支持。如果您熟悉Restler的構建過程,那麼實現它就相當簡單了。
從https://github.com/Luracast/Restler/issues/17
<?php
//jsonpformat.php
class JsonpFormat implements iFormat {
const MIME = 'text/javascript';
const EXTENSION = 'js';
/*
* JsonFormat is used internally
* @var JsonFormat;
*/
public $jsonFormat;
public static $functionName = 'parseResponse';
public function __construct() {
$this->jsonFormat = new JsonFormat();
if (isset ($_GET ['jsonp'])) {
self::$functionName = $_GET ['jsonp'];
}
}
public function getMIMEMap() {
return array (self::EXTENSION => self::MIME);
}
public function getMIME() {
return self::MIME;
}
public function getExtension() {
return self::EXTENSION;
}
public function encode($data, $human_readable = FALSE) {
return self::$functionName . '(' . $this->jsonFormat->encode ($data, $human_readable) . ');';
}
public function decode($data) {
return $this->jsonFormat->decode ($data);
}
public function setMIME($mime) {
//do nothing
}
public function setExtension($extension) {
//do nothing
}
}
?>
這應該被保存在同一目錄下restler.php文件。一旦你有了,編輯你的網關(index.php)來包含這個文件,並將它添加爲支持的格式。例如:
<?php
require_once '../../restler/restler.php';
#set autoloader
#do not use spl_autoload_register with out parameter
#it will disable the autoloading of formats
spl_autoload_register('spl_autoload');
$r = new Restler();
$r->setSupportedFormats('JsonpFormat','JsonFormat', 'XmlFormat');
$r->addAPIClass('BMI');
$r->handle();
?>
這很適合於我們: 頭( '訪問控制允許來源:*');
到控制器方法返回單個終點在此之前加入到控制器類構造函數中的一個分支,所有端點或上漲,以允許其網站廣泛。
如果您只允許某些網站訪問使用標題('Access-Control-Allow-Origin:example.com')或類似標題('Access-Control-Allow-Origin:'。$ remote_domain)。其中$ remote_domain是基於某些傳入的令牌或其他動態設置的。查看跨源資源共享(CORS),瞭解爲什麼限制*通配符的使用。
<?php
class Say {
__construct(){
header('Access-Control-Allow-Origin: *'); //Here for all /say
}
function hello($to='world') {
header('Access-Control-Allow-Origin: *'); //Here for just /say/hello
return "Hello $to!";
}
}
上述工作適用於GET和POST,其他操作需要一些來自restler的額外頭信息。下面是一些例子:
報頭( '訪問控制允許的方法:GET,POST,DELETE,PUT,OPTIONS'); header('Access-Control-Allow-Headers:whatever_headers_you_allow,header1,header2');
對於IE9和下面你將需要一個JSONP黑客。 Restler具有擴展iFormat類以包裝API輸出JASONP風格的示例。
查看Mozilla黑客獲取CORS的詳細信息。 http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ 並檢查了OPTIONS in PHP REST API
上面的作品很適合GET和POST,但其他操作需要一些作品。查看Mozilla黑客的一些細節。 HTTP://hacks.mozilla。組織/ 2009/07 /跨站點XMLHttpRequest的-與-CORS / – punkael
在這裏我要補充一點,如果由於某種原因你不想使用JSONP,你可以簡單地添加:
header('Access-Control-Allow-Origin: *');
每punkael的第一個答案(他沒有在Rester中指定在何處執行此操作)。將此行添加到sendData($ data)函數中的restler.php,其中Restler將頭數據添加到響應中。這將啓動上線378
保重然而,因爲這將允許任何域從您的API獲取數據。
- 1. Luracast Restler CI中
- 2. 爲REST API實現Ajax調用
- 3. 將用戶組添加到Luracast Restler API
- 4. Luracast restler - routes.php緩存
- 5. 如何做一個簡單的跨域AJAX調用,返回一個HTML頁面
- 6. RDFLib:使用<a href="http://www.rdflib.net/" rel="nofollow">RDFLib 3.0</a>
- 7. Luracast Restler:「命名」返回的對象
- 8. Luracast Restler身份驗證
- 9. Luracast Restler Multiformat XML not returned
- 10. 簡單的例子(使用(){}在<a href="http://www.writephponline.com/" rel="nofollow">writecodeonline</a>運行時不工作
- 11. Ajax的跨域調用REST Web服務
- 12. 如何使用<a href="http://cocoapods.org/" rel="nofollow">CocoaPods</a>使用的CocoaPods
- 13. 跨域 - REST API
- 14. restler 3跨域不起作用
- 15. 我使用的是<a href="http://www.itk.org" rel="nofollow noreferrer">ITK library</a>鏈接Windows的API
- 16. Luracast Restler是否支持多部分URI?
- 17. Luracast Restler 3日誌onRespond
- 18. AJAX a Rest api
- 19. 提交表單與我已經屈尊一個簡單的HTML表單的<a href="#">Submit</a>
- 20. 我怎樣才能使這個跨域AJAX調用到API
- 21. Django的 - Allauth - 如何通過Django的<a href="https://github.com/Tivix/django-rest-auth" rel="nofollow noreferrer">rest-auth</a>添加額外的API參數登記
- 22. 如何組成一個跨域JSON/Ajax調用一個簡單的Web服務
- 23. Yammer RESTful API和跨域Ajax調用
- 24. 使用JQuery處理錯誤當我使用JQuery <a href="http://api.jquery.com/jquery.when/" rel="nofollow">when</a>時
- 25. 使用Restler返回圖像
- 26. 返回使用基於<a href="http://neo4j.com/developer/guide-data-visualization/#_presentation_svg_based_graph_interaction" rel="nofollow">Neo4j docs</a> jquery的
- 27. 很簡單<a href link not working
- 28. GData API和跨域AJAX調用
- 29. 用於類似API的stripe.com-的時尚rocco文檔? <a href="https://stripe.com/docs/api" rel="noreferrer">https://stripe.com/docs/api</a></p> <p>我最初開始在一個簡單的文本文件記錄API的使用,而我做的進展:
- 30. 從<a href="http://projecteuler.net/" rel="nofollow">http://projecteuler.net/</a>在Python
不錯。我想知道,如果這是安全的:self :: $ _ GET ['jsonp']? –
黑客的框架是一個壞主意......這一個更好...... – danielrvt
不應該Luracast允許我們設置一個'Defaults :: $ accessControlAllowHeaders'靜態成員類型處理? – Miles