objective c - JSON POST Request on the iPhone (Using HTTP) Problems -
im having problems request asp .net mvc web service. read in thread while ago possible find out server wants the request's content-type etc. no error when compiling when actual request nothing happens , in log of server says (null) (null). there no problem doing request , fethcing objects in list. can please me irritating bug? here code:
//----------------get request 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 %@", 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 other values // nsstring* jsonrequest = [jsonobject jsonrepresentation]; // jsonstring contains 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 add example of how talk service 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); } }); });
Comments
Post a Comment