有同学在使用kbmmw 与extjs 结合的时候,涉及到了跨域访问,这个在
kbmmw 里面已经完全解决。
extjs 在访问跨域的时候,首先会使用OPIONS 调用,服务端要根据浏览器请求的
head 来回应客户端,如果正确,则客户端再调用GET 方式访问服务器。
像不像我们地下党接头一样,首先要对一下暗号,暗号正确,开始交出城防图,不正确,对不起,开枪!
服务器端的代码,kbmmw 已经替大家处理了,很简单,只是设置允许访问的信息。
[kbmMW_Method]
[kbmMW_Rest('method:get, path: "itemlist/{query}"')]
function GetItemlist([kbmMW_Rest('value: "{query}"')] const aQuery:string;
[kbmMW_Rest('value: "$ph", required: true')] const ph:string;
[kbmMW_Rest('value: "$pm", required: true')] const pm:string;
[kbmMW_Rest('value: "$xh", required: true')] const xh:string;
[kbmMW_Rest('value: "$gg", required: true')] const gg:string;
[kbmMW_Rest('value: "$th", required: true')] const th:string;
[kbmMW_Rest('value: "$jbm", required: true')] const jbm:string;
[kbmMW_Rest('value: "$gc", required: true')] const gc:string;
[kbmMW_Rest('value: "$limit", required: true')] const pagesize:string;
[kbmMW_Rest('value: "$page", required: true')] const pageindex:string;
[kbmMW_Rest('value: "$callback", required: true')] const callback:string):string;
end; implementation uses kbmMWExceptions, Unit1; {$R *.dfm} { TdgwRestService } function TdgwRestService.GetItemlist(const aQuery, ph, pm, xh, gg, th, jbm,
gc,pagesize, pageindex,callback: string): string;
begin
ProcessCORSRequest;
result := '{"result":"1","sumcount":1,"data":[{"PH":"ph001","PM":"pm001","XH":"xh001"},'
+ '{"PH":"ph002","PM":"pm002","XH":"xh002"}]}';
if callback<>'' then
Result:= callback + '(' + Result + ');'; end; procedure TdgwRestService.kbmMWCustomHTTPSmartServiceCORS(Sender: TObject;
const ARequestHelper, AResponseHelper: TkbmMWHTTPTransportStreamHelper;
const AOrigin: string; var AAllowedOrigins, AAllowedMethods,
AAllowedHeaders: string);
begin
AAllowedMethods:='GET' ;
AAllowedOrigins:='http://127.0.0.1';
AAllowedHeaders:= 'x-requested-with'; end;
运行截图
网站的源地址是http://127.0.0.1
跨域访问的地址是http://127.0.0.1:81
下面一图顶万言。
还是很简单的。