【发布时间】:2015-12-30 05:51:28
【问题描述】:
我的应用页面有一个表单,我希望用户在其中输入他的姓名和电话号码。
我希望将此数据提取到以下 URL。
变量是用户名,电话号码。
var url:NSURL = NSURL(string: "https:xyz.com/newRequest?fullname=\(username)&phone=\(phoneno)")!
这是用用户名和电话号码参数作为变量编写 URL 的正确语法吗?
这是我正在尝试实现的,但我在模拟过程中不断收到错误 405 -
var post:NSString = "username=\(username)&phoneno=\(phoneno)"
NSLog("PostData: %@",post);
var url:NSURL = NSURL(string: "https://example.com/newRequest?fullname=(username)&phone=(phoneno)")!
var postData:NSData = post.dataUsingEncoding(NSASCIIStringEncoding)!
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
【问题讨论】:
-
您忘记了源代码示例中的反斜杠。
-
我只是不确定如何在 url 字符串中使用变量组件...