【问题标题】:Cross Origin HTTP Request from CocoaHTTPServer来自 CocoaHTTPServer 的跨域 HTTP 请求
【发布时间】:2016-02-09 21:43:12
【问题描述】:

我正在使用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];

然后,当成功提供我的index.html 文件时,我从浏览器加载http://localhost:8090

然后在$(document).ready 上,我通过调用我的函数startPoll 创建一个AJAX 请求。

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);
    })
  }

然后我的HTTPServerHTTPConnection.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 发出新请求,并将该响应作为原始响应返回,但未成功。我在startPoll 中的done 回调永远不会被调用。

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;
}

有人看到我遗漏或应该尝试的任何东西吗?

【问题讨论】:

  • 你如何得到“Xcode 控制台显示这个请求进来了。”。我如何打印此日志...?

标签: javascript objective-c httprequest httpresponse cocoahttpserver


【解决方案1】:

我希望看到"Access-Control-Allow-Origin" 作为来自您的 HTTP 服务器的响应标头,而不是从您的服务器到 x-forwarded-url 的请求标头。

见:How does Access-Control-Allow-Origin header work?

【讨论】:

  • 删除仍然没有使调用成功
  • 我认为您需要将其添加到第二台服务器的响应中
  • @aahrens:你是如何得到“Xcode 控制台显示这个请求进来的。”。我如何打印此日志...?
猜你喜欢
  • 2016-04-25
  • 2011-09-09
  • 2014-04-28
  • 1970-01-01
  • 2013-03-12
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
  • 2010-09-24
相关资源
最近更新 更多