gpt4 book ai didi

javascript - 来自 CocoaHTTPServer 的跨源 HTTP 请求

转载 作者:行者123 更新时间:2023-11-28 06:23:45 31 4
gpt4 key购买 nike

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

然后我的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 发出新请求,并将该响应作为原始响应返回,但未成功。我在 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;
}

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

最佳答案

我希望将“Access-Control-Allow-Origin”视为来自 HTTP 服务器的响应 header ,而不是从服务器到 x-forwarded-url 的请求 header 。

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

关于javascript - 来自 CocoaHTTPServer 的跨源 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35302774/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com