首页 > 解决方案 > 无法从 Ionic 3 调用 Wcf Post 方法

问题描述

我正在向 ionic 请求 wcf post 方法,但出现错误

{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}

我的 WCF Post 方法是,

  public Stream GetMonthWiseAttendance(InputMonthwise input)
        {
            var SerializeObject = new JavaScriptSerializer();    
            var temp = context.MobAttendMonthWiseStudentAttaindance(input.StudentMainId, input.InstituteId, input.Yearid).ToList();
            string jsonClient = SerializeObject.Serialize(temp);
            WebOperationContext.Current.OutgoingResponse.ContentType =
                "application/json; charset=utf-8";
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,Accept");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, PUT, POST");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Max-Age", "1728000");
            return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
       }

我的离子代码是

callPost()  
 { 
    let headers = new HttpHeaders();
    headers.append('Content-Type', 'application/json');
    headers.append('Access-Control-Allow-Origin' , '*');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
   let data=JSON.stringify({userName:'Rameshwar',InstituteId:1191,Yearid:135,StudentMainId:135 });

      this.http.post("myUrl"  ,data, { headers: headers }   )
           .subscribe(res => 
                          {               
                              alert("success "+JSON.stringify(res));
                          },
                          (err) => 
                          {
                              alert('ERRROR '+JSON.stringify(err));
                           });
  } 

使用这个离子代码,我可以调用 php 的 post 方法,但调用 wcf 方法时出现错误。我还可以调用具有相同标头的 Wcf 的 Get 方法来响应

WebOperationContext.Current.OutgoingResponse.ContentType =
                "application/json; charset=utf-8";
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With,Accept");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, PUT, POST");
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Max-Age", "1728000");
            return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));

我还使用 Postman Rest 客户端检查了这个 wcf 发布请求。我从休息客户/邮递员那里得到了很好的回应。

标签: c#wcfionic-frameworkionic3

解决方案


我认为问题出在离子代码中,无需对发布数据进行字符串化,离子在内部对其进行管理。

let data=JSON.stringify({userName:'Rameshwar',InstituteId:1191,Yearid:135,StudentMainId:135 });

改成,

let data={userName:'Rameshwar',InstituteId:1191,Yearid:135,StudentMainId:135 };

希望这会奏效。


推荐阅读