如何使用POST方法向SWIFT中的服务器发送JSON数据
对于Objective C我使用此
NSMutableDictionary *get = [[NSMutableDictionary alloc]init];
[get setObject:validEmailTF.text forKey:@"email"];
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",forgetPasswordUrl]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (responseData != nil)
{
jsonDict = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(@"Values =======%@",jsonDict);
}
答案 0 :(得分:3)
你可以尝试这个,可能会帮助你
let request = NSMutableURLRequest(URL: NSURL(string: "Your forgetPasswordUrl")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
let params = ["email":validEmailTF.text] as Dictionary<String, String>
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
print("Response: \(response)")})
task.resume()
答案 1 :(得分:0)
只需将代码转换为swift,无需调整代码。
试试这个:
var get: NSMutableDictionary = NSMutableDictionary()
get["email"] = validEmailTF.text
var jsonData: NSData = NSJSONSerialization.dataWithJSONObject(get, options: kNilOptions, error: nil)
var jsonInputString: String = NSString(data: jsonData, encoding: NSUTF8StringEncoding)
var post: String = NSString(format: "r=%@",jsonInputString)
var get: NSMutableDictionary = NSMutableDictionary()
var url: NSURL = NSURL(string: "\(forgetPasswordUrl)")
var postData: NSData = post.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
var postLength: String = "\(postData.length())"
var request: NSMutableURLRequest = NSMutableURLRequest.requestWithURL(url, cachePolicy: NSURLRequestReloadIgnoringCacheData, timeoutInterval: 120.0)
request.URL = url
request.HTTPMethod = "POST"
request.setValue(postLength, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.HTTPBody = postData
var error: NSError
var response: NSURLResponse
var responseData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)
if responseData != nil {
jsonDict = NSJSONSerialization.JSONObjectWithData(responseData, options: kNilOptions, error: &error)
print("Values =======\(jsonDict)")