尝试从Xcode连接到本地服务器。我已经进口的Alamofire波德到我的Xcode项目,并在Xcode无法在Xcode中连接到本地服务器
Alamofire.request(.GET, "http://localhost:3000" , parameters: ["code": "123"]).responseJSON {
response in
print ("Hello", response)
}
iOS设备上运行时,我收到以下错误Xcode中运行以下命令。
FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x13d84f7f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost:3000/, NSErrorFailingURLKey=http://localhost:3000/, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.}
我知道当地服务正在运行。当我调用命令行下面的函数:
$ node index.js
Running at http://localhost:3000
在浏览器下面所示:
Cannot GET/
我的js文件如下:
var buffer = require('buffer');
var bodyParser = require('body-parser');
var crypto = require('crypto');
var express = require('express');
var request = require('request');
var url = require('url');
var app = express();
var config = {
clientId: '',
clientSecret: '',
callbackUrl: '',
encryptionSecret: '',
endpoint: 'https://accounts.spotify.com',
};
var secretString = config.clientId + ':' + config.clientSecret;
var authHeader = 'Basic ' + new buffer.Buffer(secretString).toString('base64');
// app.use(bodyParser.urlencoded({ extended: false })); // TODO - Figure out why this should be here
app.use(bodyParser.json()); // TODO - Figure out why this should be here
var server = app.listen(3000, function() {
var address = server.address();
console.log('Running at http://localhost:%s', address.port);
});
app.post('/swap', function(req, res) {
console.log(req.body);
if (!req.body || !req.body.hasOwnProperty('code')) {
console.log('Swap: missing auth code');
res.status(550).send('Permission Denied');
return;
}
formData = {
grant_type: 'authorization_code',
redirect_uri: config.callbackUrl,
code: req.body.code
};
console.log('Swap: POST to %s', url.resolve(config.endpoint, '/api/token'), formData);
request.post({
url: url.resolve(config.endpoint, '/api/token'),
headers: {
'Authorization': authHeader,
'Content-Type': 'application/x-www-form-urlencoded',
},
form: formData,
}, function(error, response, body) {
if (error) {
console.log('Swap: Error - ', error);
res.status(500).send('Internal Server Error');
return;
}
if (res.statusCode != 200) {
debug('Swap: response: ', response.statusCode);
res.status(550).send('Permission Denied');
return;
}
var tokenData = JSON.parse(body);
console.log('Swap: tokenData - ', tokenData);
res.status(200).set({
'Content-Type': 'application/json',
}).send(tokenData);
});
});
试着做一个正常的'GET'并发布什么得到 –
你有** https **在你的Alamofire.request中,但你实际上正在运行** http ** – Breek
只是改成** http **,仍然得到同样的错误。不完全确定你的意思是什么@VictorSigler – cnichs27