我正在尝试将数据从iOS应用发布到Node.js / Express。 我无法上班; 当我从我的应用程序尝试时,Node / Express似乎忽略了我的请求。 但是,如果我导航到我尝试在Web浏览器中POST的同一URL,则Node / Express会响应。
这是我的Node / Express代码,用于处理传入的请求:
var express = require('express'); var app = express.createServer(); app.configure(function(){ app.use(express.bodyParser()); }); app.get('/', function(req, res) { res.write('nothing to see here...'); res.end(); }); app.get('/test', function(req, res) { console.log('handling post!'); console.log(JSON.stringify(req.body)); res.end(); });这就是我试图将JSON POST到Node的方式:
// Create request data. NSString* jsonData = @"{\"test\" : \"someMoreTest\"}"; NSData* requestData = [jsonData dataUsingEncoding:NSUTF8StringEncoding]; // Create URL to POST jsonData to. NSString* urlString = @"http://www.mysite.com/test"; NSURL* url = [NSURL URLWithString:urlString]; // Create request. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody: requestData]; // Send request synchronously. NSURLResponse* response = [[NSURLResponse alloc] init]; NSError* error = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // Check result. if (error != nil) { NSLog(@"submitted request!"); } else { NSString* errorLogFormat = @"request failed, error: %@"; NSLog(errorLogFormat, error); }正如我之前提到的,如果我在浏览器中导航到http://www.mysite.com/test ,我的Node控制台输出如下: handling post! {}
但是,当我尝试从我的iOS应用程序发布时,我没有得到控制台应用程序 - 这几乎就像Node从未看到传入的POST请求。 更令人费解的是 - 我无法在iOS应用程序端获取任何错误数据 - 错误是nil 。 我在这里做错了什么想法?
I'm trying to POST data from an iOS app to Node.js/Express. I cannot get it to work; Node/Express just appears to ignore my request when I try from my app. However, if I navigate to the same URL I'm trying to POST to in a web browser, Node/Express responds.
This is my Node/Express code that handles the incoming request:
var express = require('express'); var app = express.createServer(); app.configure(function(){ app.use(express.bodyParser()); }); app.get('/', function(req, res) { res.write('nothing to see here...'); res.end(); }); app.get('/test', function(req, res) { console.log('handling post!'); console.log(JSON.stringify(req.body)); res.end(); });And this is how I'm trying to POST JSON to Node:
// Create request data. NSString* jsonData = @"{\"test\" : \"someMoreTest\"}"; NSData* requestData = [jsonData dataUsingEncoding:NSUTF8StringEncoding]; // Create URL to POST jsonData to. NSString* urlString = @"http://www.mysite.com/test"; NSURL* url = [NSURL URLWithString:urlString]; // Create request. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody: requestData]; // Send request synchronously. NSURLResponse* response = [[NSURLResponse alloc] init]; NSError* error = nil; [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // Check result. if (error != nil) { NSLog(@"submitted request!"); } else { NSString* errorLogFormat = @"request failed, error: %@"; NSLog(errorLogFormat, error); }As I mentioned earlier, if I navigate to http://www.mysite.com/test in my browser, my console output from Node is the following: handling post! {}
However, when I try to post from my iOS app, I get no console app - it's almost as if Node never sees the incoming POST request. And even more baffling - I can't get any error data on the iOS app side - the error is nil. Any ideas what I'm doing wrong here?
最满意答案
在Express的路由框架中,app.get() 仅响应GET请求,POST被忽略并可能导致404。
您可以使用post()或any()代替。
我不能评论目标C部分,但我会建议Nc -l 0.0.0.0 80 {或其他端口)o看看发生了什么。
In Express's routing framework, app.get() only responds to GET requests, POSTs are ignored and probably result in a 404.
You can use post() or any() instead.
I can't comment on the objective C portion, but I will suggest Nc -l 0.0.0.0 80 {or another port) o see what's going on.
更多推荐
发布评论