无法从我的 url 在浏览器中运行良好的 Web 服务获得响应
unable to get response from web service where my url works good in browser
我正在使用这个 url“http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails={Email_Id:mishra.bn@gmail.com,FirstName:BN,LastName:MISHRA,MobileNumber:8285547554,Password:mypass}”从服务器获取数据。但我没有得到任何回应。当我只是复制粘贴时
这个 url 到浏览器然后得到 true response.it 意味着 url 工作正常但是我的代码出错了我的代码在这里..
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"mishra.bn@gmail.com" forKey:@"Email_Id"];
[dict setObject:@"BN" forKey:@"FirstName"];
[dict setObject:@"MISHRA" forKey:@"LastName"];
[dict setObject:@"8285547554" forKey:@"MobileNumber"];
[dict setObject:@"mypass" forKey:@"Password"];
NSString *PostUrl=@"http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails=";
NSError *serr;
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&serr];
if (serr)
{
NSLog(@"Error generating json data for send dictionary...");
NSLog(@"Error (%@), error: %@", dict, serr);
return;
}
// Create request object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:PostUrl]];
request.HTTPMethod = @"POST";
request.HTTPBody = jsonData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:
[NSString stringWithFormat:@"%lu",
(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *r, NSData *data, NSError *error)
{
if (!data)
{
NSLog(@"No data returned from server, error ocurred: %@", error);
NSString *userErrorText = [NSString stringWithFormat:
@"Error communicating with server: %@", error.localizedDescription];
return;
}
NSError *deserr;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&deserr];
NSLog(@" responseDict :%@", responseDict);
}];
Thanks
你的请求看起来没问题。我想你会低于错误
Error Domain=NSCocoaErrorDomain Code=3840
JSON text did not start with array or object and option to allow fragments not set.
这是因为服务器正在响应无效的 JSON 对象。
我正在使用这个 url“http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails={Email_Id:mishra.bn@gmail.com,FirstName:BN,LastName:MISHRA,MobileNumber:8285547554,Password:mypass}”从服务器获取数据。但我没有得到任何回应。当我只是复制粘贴时
这个 url 到浏览器然后得到 true response.it 意味着 url 工作正常但是我的代码出错了我的代码在这里..
NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];
[dict setObject:@"mishra.bn@gmail.com" forKey:@"Email_Id"];
[dict setObject:@"BN" forKey:@"FirstName"];
[dict setObject:@"MISHRA" forKey:@"LastName"];
[dict setObject:@"8285547554" forKey:@"MobileNumber"];
[dict setObject:@"mypass" forKey:@"Password"];
NSString *PostUrl=@"http://test.ccsplinfo.com/services.svc/addSubscriber?userdetails=";
NSError *serr;
NSData *jsonData = [NSJSONSerialization
dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&serr];
if (serr)
{
NSLog(@"Error generating json data for send dictionary...");
NSLog(@"Error (%@), error: %@", dict, serr);
return;
}
// Create request object
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:PostUrl]];
request.HTTPMethod = @"POST";
request.HTTPBody = jsonData;
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:
[NSString stringWithFormat:@"%lu",
(unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *r, NSData *data, NSError *error)
{
if (!data)
{
NSLog(@"No data returned from server, error ocurred: %@", error);
NSString *userErrorText = [NSString stringWithFormat:
@"Error communicating with server: %@", error.localizedDescription];
return;
}
NSError *deserr;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&deserr];
NSLog(@" responseDict :%@", responseDict);
}];
Thanks
你的请求看起来没问题。我想你会低于错误
Error Domain=NSCocoaErrorDomain Code=3840
JSON text did not start with array or object and option to allow fragments not set.
这是因为服务器正在响应无效的 JSON 对象。