简体   繁体   中英

Alamofire http request to local webserver

I'm developing an iPhone app and the plan is to send a JSON packet every so often from the app to a local webserver. To do this, I had planned to use Alamofire . My POST method looks like this:

Alamofire.request(Alamofire.Method.POST, "http://XXX.XX.X.XX:3000/update", parameters: dictPoints, encoding: .JSON)
    .responseJSON {(request, response, JSON, error) in
    println(JSON)
}

The IP address is marked out, but I've made sure that this corresponds to the IPv4 wireless address of my local server. The server is set to listen to port 3000. The server configuration looks like this:

var express = require('express');
var app = express();
var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
  console.log("MongoDB connection is open.");
});

// Mongoose Schema definition
var Schema = mongoose.Schema;
var LocationSchema = new Schema({
    //some schema here
});

// Mongoose Model definition
var LocationsCollection = mongoose.model('locations', LocationSchema);

// URL management
app.get('/', function (req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.get('/update', function (req, res) {
    console.log("Got something from the phone!");
});

// Start the server
var server = app.listen(3000, function () {
  var host = server.address().address
  var port = server.address().port
  console.log('App listening at %s:%s',host, port)
})

So, this server seems to work ok. I can test it in my browser and type the URL: http://127.0.0.1:3000 and it will feed me the index.html file. If I type in http://127.0.0.1:3000/update ... then I get the "Got something from the phone!" message. However, when I run my app (making sure my phone is on the same wireless network as the server) and the Alamofire method gets called... the response I get is nil. I also don't see the "Got something from the phone!" message. Can anyone let me know why that would be happening... or better yet, how to fix it?

A few thoughts:

  1. You are creating a POST request in Alamofire, but you've told Express to handle GET requests for /update . Use app.post(...) if you want to handle it as a POST .

  2. Your Alamofire code is looking for JSON response, but you don't appear to be creating a JSON response. In the short term, you could use responseString rather than responseJSON , but I presume you really want to change your web service to respond with JSON (to make it easier for the app to parse the responses).

  3. Your Alamofire code sending a JSON request (but clearly when you send a request via the web browser, it's not JSON). Are you sure you wanted to send JSON request? (This is not to be confused with the JSON response issue.) Did you want to use the .URL encoding type parameter, rather than .JSON ?

  4. Whenever you have a request that works correctly from a web browser, but not from the app, it's useful to watch both using a tool like Charles and you can then compare how they differ and diagnose the source of the different behavior.

如果您在同一台计算机上运行Express服务器和iOS模拟器,请尝试使用http://0.0.0.0:<port>/<url>而不是实际IP,这在我的情况下有所帮助。

It sounds like the node server is not bound to the address you expect it to be. You should verify that when you type in the actual IP address in your " http://XXX.XX.X.XX:3000/update " in your web browser that it responds. Your question details suggest you've just been using the loopback address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM