[英]Alamofire error when creating Stripe customer ID (iOS)
I am using Alamofire and Swift in my Heroku app using Node.js (integrating Stripe). 我在使用Node.js(集成Stripe)的Heroku应用程序中使用Alamofire和Swift。 However, when creating a customer ID, I get the following error. 但是,创建客户ID时,出现以下错误。 My code sets are below. 我的代码集如下。
Let me know if any other details are required to know. 让我知道是否需要其他详细信息。
Error message: Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength) 错误消息:Alamofire.AFError.responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength)
Client side code 客户端代码
func createCustomerKey(withAPIVersion apiVersion: String, completion: @escaping STPJSONResponseCompletionBlock) {
let customerIDURL = self.baseURL.appendingPathComponent("customer")
let customerIDParameters = ["email":(Auth.auth().currentUser?.email)!]
Alamofire.request(customerIDURL, method: .post, parameters: customerIDParameters, encoding: JSONEncoding.default).validate(statusCode: 200..<300).responseJSON { responseJSON in
switch responseJSON.result {
case .success(let json):
completion(json as? [String:AnyObject], nil)
print("\(json)\n\n\n\n")
case .failure(let error):
completion(nil, error)
print("Error message:\(responseJSON.result.error)")
break
}
}
Server side code 服务器端代码
const express = require('express')
const path = require('path')
const PORT = process.env.PORT || 5000
var app = express();
var stripe = require('stripe')('secret_key')
var bodyParser = require('body-parser')
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
app.post('/customer', (req, res) => {
var email = req.body.email;
stripe.customers.create({
email: email
}, function(err) {
if (err) {
console.log(err, req.body)
res.status(500).end()
} else {
res.status(200).send()
}
}
The error message means that Alamofire is failing when processing the response JSON. 该错误消息表示在处理响应JSON时Alamofire失败。
Specifically, the issue seems to be on the server side - as far as I can tell you are not sending a valid JSON response back from the server, just a status code of 200. 具体来说,问题似乎在服务器端-据我所知,您不是从服务器发送回有效的JSON响应,只是状态码为200。
To send back data you'll need to supply a parameter to res.send(), for example: 要发回数据,您需要为res.send()提供一个参数,例如:
res.status(200).send({'email':email})
Also, the Stripe API docs state that the callback function receives two parameters, the second being the customer object. 此外, Stripe API文档指出,回调函数接收两个参数,第二个是客户对象。 So the code could be implemented as such: 因此,代码可以这样实现:
stripe.customers.create({
email: email
}, function(err, customer) {
if (err) {
res.status(500).end()
} else {
res.status(200).send(customer)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.