我使用CocoaHTTPServer
来托管我自己的HTML
文件。在index.html
我试图对另一个资源执行请求,但我无法成功执行此资源。这是我目前的设置。
self.httpServer = [[HTTPServer alloc] init];
[self.httpServer setType:@"_http._tcp."];
[self.httpServer setPort:8090];
NSString *webPath = [[NSBundle mainBundle].resourcePath stringByAppendingPathComponent:@"Web"];
[self.httpServer setDocumentRoot:webPath];
然后,当我成功提供http://localhost:8090
文件时,我从浏览器加载index.html
。
然后在$(document).ready
上,我通过调用我的函数AJAX
创建startPoll
请求。
proxyAjax: function ( opts ) {
var proxyOpts = _.cloneDeep(opts);
if (!proxyOpts.headers) proxyOpts.headers = {};
proxyOpts.headers['x-forwarded-url'] = proxyOpts.url;
proxyOpts.url = new URI({
protocol: 'http',
hostname: 'localhost',
port: 8090,
path: '/'
}).toString();
proxyOpts.type = proxyOpts.type || proxyOpts.method;
console.log("PROXYING:", proxyOpts);
return $.ajaxDigest(proxyOpts);
},
// Attempt an API call to the camera on a short timeout.
// Call the callback when we have a non-timeout response.
startPoll: function ( callback ) {
var self = this;
console.log("Polling...");
this.proxyAjax({
url: 'http://192.168.123.1/deviceinfo',
username: 'admin',
password: 'password',
timeout: 2000
})
.done( function ( data, textStatus, jqXHR ) {
console.log("Poll success:", arguments);
callback(jqXHR);
})
}
然后我的HTTPServer
通过此方法HTTPConnection.m
- (void)replyToHTTPRequest
中的请求
Xcode控制台显示此请求。
Received HTTP request:
GET / HTTP/1.1
Host: localhost:8090
X-Requested-With: XMLHttpRequest
Accept-Language: en-US,en;q=0.8
x-forwarded-url: http://192.168.123.1/deviceinfo
Accept: */*
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5
Referer: http://localhost:8090/
Accept-Encoding: gzip, deflate, sdch
Connection: keep-alive
现在,我尝试向x-forwarded-url
发出新请求,并将该响应作为原始响应失败。我永远不会调用done
中的startPoll
回调。
if ([[request headerField:@"x-forwarded-url"] length] > 0) {
NSURL *url = [[NSURL alloc] initWithString:[request headerField:@"x-forwarded-url"]];
HTTPMessage *remoteRequest = [[HTTPMessage alloc] initRequestWithMethod:@"HEAD" URL:url version:[request version]];
[remoteRequest setHeaderField:@"Access-Control-Allow-Origin" value:@"*"];
[remoteRequest setHeaderField:@"Access-Control-Allow-Methods" value:@"GET,PUT,POST,DELETE,OPTION"];
[remoteRequest setHeaderField:@"Access-Control-Allow-Headers" value:@"Accept, Origin, Content-Type"];
[remoteRequest setHeaderField:@"HOST" value:url.host];
NSData *requestData = [remoteRequest messageData];
NSString *responseString = [[NSString alloc] initWithData:requestData encoding:NSUTF8StringEncoding];
APILogVerbose(@"%@[%p]: HTTP Request:\n%@", THIS_FILE, self, responseString);
[asyncSocket writeData:requestData withTimeout:TIMEOUT_WRITE_HEAD tag:HTTP_REQUEST_HEADER];
return;
}
任何人都会看到我遗失或应该尝试的任何内容?
答案 0 :(得分:0)
我希望将"Access-Control-Allow-Origin"
视为来自HTTP服务器的响应标头,而不是从服务器到x-forwarded-url的请求标头。