如何从 UITextField 捕获表情符号并将其作为 JSON 通过 POST 传递到后端 (node.js)
How to capture Emoji from UITextField and pass it as JSON via POST to backend (node.js)
我正在尝试从 UITextField 中获取用户输入的文本或表情符号,并将其存储在 NSMutableDictionary 中。这个 NSMutableDictionary 然后通过 HTTP POST 发送到我的后端服务器 (node.js)。后端依次发出 REST API 调用以将消息以 UTF-8 格式发送给收件人。
此过程非常适合英语中的常规文本。但是,如果我输入表情符号字符,则会失败。
NSString *msg = self.textFieldMessage.text;
NSString *test = [[NSString alloc] initWithData:msg encoding:NSUTF8StringEncoding];
//NSString *msgUtf = [msg UTF8String];
NSLog(@"User entered this %@",self.textFieldMessage.text);
NSMutableDictionary *messageData = [[NSMutableDictionary alloc] init];
[messageData setValue:msg forKey:@"message"];
[messageData setValue:@"testRecipient" forKey:@"recipient"];
[self.textFieldMessage setText:@""];
[self.labelForPush setText:msg];
NSDictionary *temp = [apiCallFromTempViewController sendMessage: messageData];
// this api call does an HTTP POST of the NSMutable Dictionary to my backend
将 Emoji (NSString) 转换为 NSMutableDictionary 时似乎失败了。这是 Xcode 中的错误消息:
Unknown error occured,Please try again later
2015-09-04 02:15:38.822 SecureText[6602:765414] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x170668a00 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
后端收到的 HTTP POST 也报告此调用存在问题。以下是我的 node.js 后端吐出的错误:
SyntaxError: Unexpected end of input
at Object.parse (native)
at parse (/home/azureuser/node_modules/body-parser/lib/types/json.js:88:17)
at /home/azureuser/node_modules/body-parser/lib/read.js:108:18
请指教如何在 UITextfield 中捕获 text/emoji 并将其传递到 NSDictionary。
提前致谢。
不用改编码直接用self.textFieldMessage.text
就可以了。 NSString
在 JSON 中有效,并且通过更改字符串编码,您将破坏表情符号。
我正在尝试从 UITextField 中获取用户输入的文本或表情符号,并将其存储在 NSMutableDictionary 中。这个 NSMutableDictionary 然后通过 HTTP POST 发送到我的后端服务器 (node.js)。后端依次发出 REST API 调用以将消息以 UTF-8 格式发送给收件人。
此过程非常适合英语中的常规文本。但是,如果我输入表情符号字符,则会失败。
NSString *msg = self.textFieldMessage.text;
NSString *test = [[NSString alloc] initWithData:msg encoding:NSUTF8StringEncoding];
//NSString *msgUtf = [msg UTF8String];
NSLog(@"User entered this %@",self.textFieldMessage.text);
NSMutableDictionary *messageData = [[NSMutableDictionary alloc] init];
[messageData setValue:msg forKey:@"message"];
[messageData setValue:@"testRecipient" forKey:@"recipient"];
[self.textFieldMessage setText:@""];
[self.labelForPush setText:msg];
NSDictionary *temp = [apiCallFromTempViewController sendMessage: messageData];
// this api call does an HTTP POST of the NSMutable Dictionary to my backend
将 Emoji (NSString) 转换为 NSMutableDictionary 时似乎失败了。这是 Xcode 中的错误消息:
Unknown error occured,Please try again later
2015-09-04 02:15:38.822 SecureText[6602:765414] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x170668a00 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
后端收到的 HTTP POST 也报告此调用存在问题。以下是我的 node.js 后端吐出的错误:
SyntaxError: Unexpected end of input
at Object.parse (native)
at parse (/home/azureuser/node_modules/body-parser/lib/types/json.js:88:17)
at /home/azureuser/node_modules/body-parser/lib/read.js:108:18
请指教如何在 UITextfield 中捕获 text/emoji 并将其传递到 NSDictionary。
提前致谢。
不用改编码直接用self.textFieldMessage.text
就可以了。 NSString
在 JSON 中有效,并且通过更改字符串编码,您将破坏表情符号。