繁体   English   中英

POST到WCF服务时,iOS 5 Json错误400

[英]iOS 5 Json error 400 when doing a POST to WCF service

该get可以正常工作,并且可以使用测试winform应用程序发布到数据库,但是在尝试从iOS应用程序执行POST时从IIS日志中收到错误400。 这是IIS日志2012-06-28 12:13:39 192.168.100.112 POST /JsonWcfService/GetEmployees.svc/json/updateuser-58129-192.168.100.231 WcfTest / 1.0 + CFNetwork / 548.0.3 + Darwin / 11.4.0 400 0 0 4163

这是WCF服务代码

    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "json/updateuser")]
    //method
     Employee PostEmp(Employee emp);    

[DataContract]
public class Employee
{
    [DataMember]
    public string firstname { get; set; }
    [DataMember]
    public string lastname { get; set; }
    [DataMember]
    public decimal salary { get; set; }
    [DataMember]
    public int idkey { get; set; }
    public Employee()
    {

    }
}

public Employee GetEmp(int IDKey)
{
    Employee emp = new Employee();

    using (EmpDBEntities empContext = new EmpDBEntities())
    {
        var j = (from t in empContext.EMPKeys where t.IDKey == IDKey select t).FirstOrDefault();
        emp = (new Employee(j.FirstName, j.LastName, j.Salary, j.IDKey));
        return emp;
    }
 }

xcode来自该网站上的另一个帖子。

NSArray *keys = [NSArray arrayWithObjects:@"idkey", @"firstname", @"lastname",@"salary", nil];
NSArray *objects = [NSArray arrayWithObjects:@"1", @"jim", @"jones", @"450",nil];
NSData *__jsonData = nil;
NSString *__jsonString = nil;
NSURL *url = [NSURL
URLWithString:@"http://<ip address>/JsonWcfService/GetEmployees.svc/json/updateuser"];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
    __jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
    __jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
}

// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: __jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];

NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];

if (errorReturned) {
    // Handle error.
}
else
{
    NSError *jsonParsingError = nil;
    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments

错误:&jsonParsingError]; }

问题是实施文件更改了两行,现在可以正常工作了。

[OperationContract]
[WebInvoke(Method = "POST",
   ResponseFormat = WebMessageFormat.Json,
   RequestFormat = WebMessageFormat.Json,
   //BodyStyle = WebMessageBodyStyle.Wrapped, removed this line otherwise the Employee object was null
   //UriTemplate = "json/updateuser")
UriTemplate =""] //removed "json/updateuser and was able to post to the DB
//method
 Employee PostEmp(Employee emp);    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM