【问题标题】:Unable to call Wcf Post Method From Ionic 3无法从 Ionic 3 调用 Wcf Post 方法
【发布时间】:2018-10-15 15:12:49
【问题描述】:

我正在向 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# wcf ionic-framework ionic3


    【解决方案1】:

    我认为问题出在 Ionic Code 中,不需要 stringfy 发布数据,ionic 内部管理它。

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

    改成,

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

    希望这会奏效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-05
      • 1970-01-01
      • 2019-07-11
      • 2012-08-07
      • 2021-02-16
      • 2014-10-25
      • 1970-01-01
      • 2016-11-22
      相关资源
      最近更新 更多