发送JSON数据给服务器
2016-01-29 09:45
609 查看
需要将JSON格式的数据传送给服务器,注意需要设置: [request setValue:@”application/json” forHTTPHeaderField:@”Content-Type”];
Content-Type类型为:application/json
Content-Type类型为:application/json
// 1.URL NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"]; // 2.请求 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 3.请求方法 request.HTTPMethod = @"POST"; // 4.设置请求体(请求参数) // 创建一个描述订单信息的JSON数据 NSDictionary *orderInfo = @{ @"shop_id" : @"1243324", @"shop_name" : @"啊哈哈哈", @"user_id" : @"899" }; NSData *json = [NSJSONSerialization dataWithJSONObject:orderInfo options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = json; // 5.设置请求头:这次请求体的数据不再是普通的参数,而是一个JSON数据 [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; // 6.发送请求 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (data == nil || connectionError) return; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; NSString *error = dict[@"error"]; if (error) { [MBProgressHUD showError:error]; } else { NSString *success = dict[@"success"]; [MBProgressHUD showSuccess:success]; } }];
相关文章推荐
- html5 js canvas中画星星的函数
- JavaScript判断DIV内容是否为空的方法
- 基于javascript实现listbox左右移动
- 对象 遍历 ( for in )
- js实现滚动条滚动到某个位置便自动定位某个tr
- underscorejs-every学习
- underscorejs-reject学习
- js面向对象基础
- js使用面向对象实现选项卡
- JavaScript中的this,call,apply使用及区别详解
- javascript实现tab响应式切换特效
- js获取前几天日期
- 归并排序c++和javascript实现
- javascript Math.pow 函数 详解 【附】年均增长率计算
- 原生JavaScript实现跨域
- #学习笔记#(28)+JS瀑布流-滚动条下拉加载图片
- js中的typeof
- three.js 源码注释(四十四)Light/DirectionalLight.js
- javascript实现tab响应式切换特效
- JavaScript中的this,call,apply使用及区别详解