我有以下字典:
NSDictionary *param = @{@"schoolid":@"schooldb1",
@"token":del.tokenString,
@"mobile":del.phoneString
};
NSLog(@"param:%@",param);
我想将此参数(schoolid,token,mobile)发送到Web视图。但我不知道如何发送。我试图在互联网上搜索,但我没有为我的问题找到任何正确的解决方案。
我的主要网址是:
NSString *url=@"https://MyURL.com/School/AppSingleTrack";
我将打电话给UIWebview,如下所示:
NSString *finalurl=[NSString stringWithFormat:@"https://MyURL.com/School/AppSingleTrack/?%@",param];
NSURL *nsurl=[NSURL URLWithString:finalurl];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webview loadRequest:nsrequest];
[self.view addSubview:_webview];
答案 0 :(得分:1)
试试这个,
NSDictionary *param = @{@"schoolid":@"schooldb1",
@"token":del.tokenString,
@"mobile":del.phoneString
};
NSLog(@"param:%@",param);
NSString *url=@"https://24x7tracker.com/School/AppSingleTrack";
NSString *finalurl=[NSString stringWithFormat:@"https://24x7tracker.com/School/AppSingleTrack/"];
NSURL *nsurl=[NSURL URLWithString:finalurl];
NSMutableURLRequest *nsrequest=[NSMutableURLRequest requestWithURL:nsurl];
NSData *data = [NSJSONSerialization dataWithJSONObject:param options:0 error:nil];
[nsrequest setHTTPBody:data];
[_webview loadRequest:nsrequest];
[self.view addSubview:_webview];
如果需要[nsrequest setHTTPMethod:@"GET"];
或POST
以及内容类型等,请设置请求的必要条件。
您应该使用AFNetworking
,这样会更容易。
答案 1 :(得分:0)
使用此代码,
NSString *sUrl = @"https://24x7tracker.com/School/AppSingleTrack";
NSMutableURLRequest *res = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sUrl]];
[res setHTTPMethod:@"POST"];
NSDictionary *params; = [NSDictionary dictionaryWithObjectsAndKeys:
@"schooldb1",@"schoolid",
del.tokenString,@"token",
del.phoneString,@"mobile",
nil];
NSMutableArray *pairArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in params)
[pairArray addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]];
[res setHTTPBody:[[pairArray componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:res
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSLog(@"request URL : %@",res.URL);
NSLog(@"request Method : %@",res.HTTPMethod);
NSLog(@"parameters : %@",params);
NSLog(@"response : %@",response);
Resp *r = [ [Resp alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:nil]];
// Success - Show Sucess message
if ([r.sCode isEqualToString:@"success"]) {
NSLog(@"response message : %@",r.sData);
}
}];
使用Class Resp:
Resp.h
#import <Foundation/Foundation.h>
@interface Resp : NSObject
@property (nonatomic, copy) NSString *sCode;
@property (nonatomic, copy) NSString *sMessage;
@property (nonatomic, copy) NSString *sData;
- (id)initWithDictionary:(NSDictionary *)dictionary;
@end
Resp.m
#import "Resp.h"
@implementation Resp
@synthesize sCode = _id;
@synthesize sMessage = _title;
@synthesize sData = _data;
- (id)initWithDictionary:(NSDictionary *)dictionary {
self = [super init];
if (self) {
self.sCode = [dictionary objectForKey:@"code"];
self.sMessage = [dictionary objectForKey:@"message"];
self.sData = [dictionary objectForKey:@"data"];
}
return self;
}
@end
然后终于得到了答复,希望其有用
答案 2 :(得分:0)
首先,与您的同事交谈或查看URL的文档,确认API需要的参数格式以及请求方法,例如GET或POST。
其次,将params连接到正确的格式,不要忘记转义参数。
如果您的网址需要正常参数,请尝试以下操作:
NSDictionary *params = @{@"schoolid" : @"",
@"token" : @"",
@"mobile" : @""};
NSMutableArray *keyValues = [NSMutableArray array];
for (NSString *key in params) {
[keyValues addObject:[NSString stringWithFormat:@"%@=%@&", key, params[key]]];
}
NSString *paramsString = [keyValues componentsJoinedByString:@"&"];
paramsString = [paramsString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
// Don't add / at last unless the URL has, because / is another path
NSString *baseURL = @"https://24x7tracker.com/School/AppSingleTrack";
// If GET, you can use these two lines, or use below
// NSString *urlString=[NSString stringWithFormat:@"%@?%@", baseURL, paramsString];
// NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
// if POST
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseURL]];
request.HTTPMethod = @"POST"; // Default is GET, you can send get request by default,
request.HTTPBody = [paramsString dataUsingEncoding:NSUTF8StringEncoding];
[webView loadRequest:request];
编辑: 根据@ Shubhank的猜测,如果webview的请求是通过ajax,你应该确认javascript的功能,并在webview的委托webViewDidFinishLoad中尝试这些代码:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"javascriptFunction(%@, %@, %@)", param1, param2, param3]];
}