iPhone 上的 JSON POST 请求(使用 HTTP)问题
我对 asp .net mvc Web 服务的请求遇到问题。我不久前在一个线程中读到,可以找出服务器想要请求的内容类型等。编译时我没有收到错误,但当我执行实际请求时,服务器日志中什么也没有发生它只说(空)(空)。执行 GET 请求并获取列表中的所有对象是没有问题的。谁能帮我解决这个令人恼火的错误吗?这是代码:
//----------------GET request to webservice works fine----------------------------------------
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod: @"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *stringResponse = [[NSString alloc] initWithData: response encoding: NSUTF8StringEncoding];
//NSLog(@"stringResponse is %@", stringResponse);
//--------------------------------------------------------------------------------------------
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject= [output JSONValue];
NSLog(@"TWITTER: %@",theObject);
*/
//--------------------------------------------------------------------------------------------
NSURL *url = [NSURL URLWithString:@"http://errorreport.abou.se/Errors/1.0"];
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\"}";
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\",\"Address\":\"smedjegatan\",\"StreetNumber\":\"34\",\"Feedback\":\"True\",\"FeedbackWay\":\"Telefon\"}";
NSMutableDictionary* jsonObject = [NSMutableDictionary dictionary];
//NSMutableDictionary* metadata = [NSMutableDictionary dictionary];
//[metadata setObject:@"NewLoc" forKey:@"Uri"];
//[metadata setObject:@"Location.NewLoc" forKey:@"Type"];
//[jsonObject setObject:metadata forKey:@"__metadata"];
[jsonObject setObject:@"Gurras" forKey:@"Description"];
[jsonObject setObject:@"Klotter" forKey:@"Category"];
[jsonObject setObject:@"smedjegatan" forKey:@"Address"];
[jsonObject setObject:@"34" forKey:@"StreetNumber"];
[jsonObject setObject:@"True" forKey:@"Feedback"];
[jsonObject setObject:@"Telefon" forKey:@"FeedbackWay"];
// ... complete the other values
//
NSString* jsonRequest = [jsonObject JSONRepresentation];
// jsonString now contains your example strings.
NSLog(@"Request: %@", jsonRequest);
//NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
//[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"returnData: %@", returnString);
我还可以添加一个如何使用 javascript 与服务通信的示例:
<script type="text/javascript">
var obj = { "Description": "det kanske funkar" };
$(document).ready(function () {
$.ajax({
type: "POST",
url: "/Errors/1.0",
dataType: "json",
contentType: "application/json",
processData: true,
data: '{"Description": "STeffeent asdasd", "Category": "Miljö", "Address": "Bogatan","StreetNumber": "14", "Feedback": "true", "FeedbackWay": "Brev"}',
success: function (data) {
$("#result").text(data.Description);
}
});
});
Im having problems with my request to a asp .net mvc web service. I read in a thread a while ago that its possible to find out what the server wants the the request's content-type to be etc. I get no error when compiling but when I do the actual request nothing happens and in the log of the server it only says (null) (null). There is no problem doing the GET request and fethcing all objects that are in the list. Can anyone please help me with this irritating bug? here is the code:
//----------------GET request to webservice works fine----------------------------------------
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: url];
[request setHTTPMethod: @"GET"];
NSData *response = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
NSString *stringResponse = [[NSString alloc] initWithData: response encoding: NSUTF8StringEncoding];
//NSLog(@"stringResponse is %@", stringResponse);
//--------------------------------------------------------------------------------------------
NSString *twitterTrendsUrl=@"http://search.twitter.com/trends.json";
NSString *output=
[NSString stringWithContentsOfURL:[NSURL URLWithString:twitterTrendsUrl]];
id theObject= [output JSONValue];
NSLog(@"TWITTER: %@",theObject);
*/
//--------------------------------------------------------------------------------------------
NSURL *url = [NSURL URLWithString:@"http://errorreport.abou.se/Errors/1.0"];
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\"}";
//NSString *jsonRequest = @"{\"Description\":\"Gurras Description\",\"Category\":\"Klotter\",\"Address\":\"smedjegatan\",\"StreetNumber\":\"34\",\"Feedback\":\"True\",\"FeedbackWay\":\"Telefon\"}";
NSMutableDictionary* jsonObject = [NSMutableDictionary dictionary];
//NSMutableDictionary* metadata = [NSMutableDictionary dictionary];
//[metadata setObject:@"NewLoc" forKey:@"Uri"];
//[metadata setObject:@"Location.NewLoc" forKey:@"Type"];
//[jsonObject setObject:metadata forKey:@"__metadata"];
[jsonObject setObject:@"Gurras" forKey:@"Description"];
[jsonObject setObject:@"Klotter" forKey:@"Category"];
[jsonObject setObject:@"smedjegatan" forKey:@"Address"];
[jsonObject setObject:@"34" forKey:@"StreetNumber"];
[jsonObject setObject:@"True" forKey:@"Feedback"];
[jsonObject setObject:@"Telefon" forKey:@"FeedbackWay"];
// ... complete the other values
//
NSString* jsonRequest = [jsonObject JSONRepresentation];
// jsonString now contains your example strings.
NSLog(@"Request: %@", jsonRequest);
//NSURL *url = [NSURL URLWithString:@"https://mydomain.com/Method/"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"json" forHTTPHeaderField:@"Data-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
//[NSURLConnection connectionWithRequest:[request autorelease] delegate:self];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSLog(@"returnData: %@", returnString);
I can also add an example of how to talk to the service with javascript:
<script type="text/javascript">
var obj = { "Description": "det kanske funkar" };
$(document).ready(function () {
$.ajax({
type: "POST",
url: "/Errors/1.0",
dataType: "json",
contentType: "application/json",
processData: true,
data: '{"Description": "STeffeent asdasd", "Category": "Miljö", "Address": "Bogatan","StreetNumber": "14", "Feedback": "true", "FeedbackWay": "Brev"}',
success: function (data) {
$("#result").text(data.Description);
}
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论