Node.js / Express忽略iOS JSON POST(Node.js / Express ignores iOS JSON POST)

编程入门 行业动态 更新时间:2024-10-25 06:22:34
Node.js / Express忽略iOS JSON POST(Node.js / Express ignores iOS JSON POST)

我正在尝试将数据从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.

更多推荐

本文发布于:2023-08-07 09:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Express   js   Node   iOS   ignores

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!