Node.js 无法从Alamofire向ExpressJs发送POST请求
我使用NodeJs/ExpressJs作为后端服务器(ubuntu18.04.3lts),当我从前端(JavaScript)或邮递员发出ajaxget或POST请求时,它工作得非常好(如预期的那样)。 当我使用Alamofire(swift)发出GET请求时,这也会按预期工作,但当我从Alamofire发出POST请求时,会将空数据发送(或接收)到NodeJs/ExpressJs服务器 在我的NodeJs服务器中,我使用FormData解析body(FormData),在我的前端(JavaScript)中,我使用FormData api和标题,Node.js 无法从Alamofire向ExpressJs发送POST请求,node.js,express,mongoose,alamofire,formidable,Node.js,Express,Mongoose,Alamofire,Formidable,我使用NodeJs/ExpressJs作为后端服务器(ubuntu18.04.3lts),当我从前端(JavaScript)或邮递员发出ajaxget或POST请求时,它工作得非常好(如预期的那样)。 当我使用Alamofire(swift)发出GET请求时,这也会按预期工作,但当我从Alamofire发出POST请求时,会将空数据发送(或接收)到NodeJs/ExpressJs服务器 在我的NodeJs服务器中,我使用FormData解析body(FormData),在我的前端(JavaScr
content-type:false
发送数据。
当我从Alamofire向我的后端服务器发送POST请求时,我在其中设置了头
func uploadPostWithImageAndText() {
let parameters = [
"shared" : "Public",
"post": self.textView.text
] as [String : Any]
let recovedUserJsonData = UserDefaults.standard.object(forKey: "userData")
let recovedUserJson = NSKeyedUnarchiver.unarchiveObject(with: recovedUserJsonData as! Data) as! NSDictionary
// print(recovedUserJson)
var token = recovedUserJson.value(forKey: "token") as! String
token = String(format: "%@", token)
// print(token)
let headers = [
"Authorization" : token
]
let imgData = imgView.image?.jpegData(compressionQuality: 0.2)
Alamofire.upload(multipartFormData: { multipartFormData in
//Parameter for Upload files
multipartFormData.append(imgData!, withName: "fileUpload",fileName: "fileUpload" , mimeType: "image/png")
for (key, value) in parameters
{
multipartFormData.append(key.data(using: .utf8)!, withName: value as! String)
}
}, usingThreshold:UInt64.init(),
to: "https://apis.example.com/apis/feed/create-post", //URL Here
method: .post,
headers: headers, //pass header dictionary here
encodingCompletion: { (result) in
switch result {
case .success(let upload, _, _):
print("the status code is :")
upload.uploadProgress(closure: { (progress) in
print("something")
})
upload.responseJSON { response in
print("the resopnse code is : \(response.response?.statusCode)")
print("the response is : \(response)")
}
break
case .failure(let encodingError):
print("the error is : \(encodingError.localizedDescription)")
break
}
})
在ExpressJs中,我有FormData的主体解析器
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(bodyParser.json({ limit: '50mb' }));
我正在使用强大的模块来解析body
我进阿拉莫菲尔时出错了
<4> load failed with error Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={_kCFStreamErrorCodeKey=-2102, NSUnderlyingError=0x60000193f150 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <16AA23FC-8DFC-4598-B032-96636892F2B7>.<4>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
"LocalDataTask <16AA23FC-8DFC-4598-B032-96636892F2B7>.<4>"
), NSLocalizedDescription=The request timed out., NSErrorFailingURLStringKey=https://apis.example.com/apis/feed/create-post, NSErrorFailingURLKey=https://apis.example.com/apis/feed/create-post, _kCFStreamErrorDomainKey=4} [-1001]
我不明白为什么在后端(api端点)接收到空数据,谢谢。我用nodemon替换了pm2,然后开始工作,我想问题出在pm2上。我用nodemon替换了pm2,然后开始工作,我想问题出在pm2上
fields {}