2015-01-19 8 views
0

SAPUI5를 사용하고 있으며 Json Model을 사용하여 REST 서비스에 데이터를 보내려는 XML 양식이 있습니다.sapui5를 사용하여 REST 서비스를 통해 json 모델을 보내는 방법

내 응용 프로그램을 만들기 위해 SAPUI5 MVC 모델을 사용하고 있습니다.

REST와 JSON을 사용하여 서버에 데이터를 보내려면 어떻게해야합니까?

sap.ui.controller("controller.NewTicket", { 

    onInit: function() { 
     this.router = sap.ui.core.UIComponent.getRouterFor(this); 
     this.router.attachRoutePatternMatched(this._handleRouteMatched, this); 
    }, 
    _handleRouteMatched:function(evt){ 
     if("NewTicket" !== evt.getParameter("name")){ 
      return; 
     } 
     var id = evt.getParameter("arguments").id; 
     var model = new sap.ui.model.json.JSONModel({id:id}); 
     this.getView().setModel(model,"data"); 
    }, 


    enviar:function() { 
     jQuery.sap.require("sap.m.MessageBox"); 

     // open a fully configured message box 
     sap.m.MessageBox.show("Confirmar a abertura do chamado?", 
       sap.m.MessageBox.Icon.QUESTION, 
       "Confirmar", 
       [sap.m.MessageBox.Action.YES, sap.m.MessageBox.Action.NO], 
       function(sResult) { 
      if(sResult == sap.m.MessageBox.Action.YES)  
      { 
       var oModel = new sap.ui.model.json.JSONModel(); 
       var aData = jQuery.ajax({ 
        type : "POST", 
        contentType : "application/json", 
        url : "http://192.168.0.32:9082/maxrest/rest/mbo/sr/?description="+ **deviceModel.sr.description** +"&_format=json&_compact=true&_verbose=true", 
        dataType : "json", 
        async: false, 
        success : function(data,textStatus, jqXHR) { 
         oModel.setData({modelData : data}); 
         sap.m.MessageBox.show("ABRIU"); 
        }, 
        error : function(data,textStatus, jqXHR) { 
         oModel.setData({modelData : data}); 
         sap.m.MessageBox.show(textStatus); 
        } 
      })} 
      else 
      { 
      } 
     }, 
       sap.m.MessageBox.Action.YES); 

     var deviceModel = new sap.ui.model.json.JSONModel({ 
      sr : [{ 
       description: "", 
       long_description: "" 
      }]}); 
     deviceModel.setDefaultBindingMode("TwoWay"); 
     sap.ui.getCore().setModel(deviceModel); 

     jQuery.sap.require("sap.m.MessageToast"); 
     sap.m.MessageToast.show(deviceModel.getData().sr.description); 
    } 

}); 

그리고보기

... 나는 그것이 "JSON"유형의 하나로, OData 모델을 사용하는 것이 더 쉽습니다 것을 발견 내 경험에서
<mvc:View xmlns:mvc="sap.ui.core.mvc" displayBlock="true" xmlns="sap.m" xmlns:co="sap.ui.commons" 
xmlns:f="sap.ui.layout.form" xmlns:core="sap.ui.core" controllerName="com.maximo.controller.NewTicket"> 

<Page id="NewTicket" enableScrolling="true" title="{i18n>newTicket}" > 
    <content> 
      <f:SimpleForm > 
       <core:Title level="H5" 
        text="O chamado será aberto em seu nome e você será o usuário afetado"/> 
       <Label text="Resumo"/> 
       <Input type="Text" maxLength="100" value="{/sr/description}"/>  
       <Label text="Detalhes"/>  
       <TextArea height="50%" cols="800" value="{/sr/long_description}"/> 
      </f:SimpleForm> 
    </content> 
    <customHeader> 
     <Bar> 
      <contentLeft>   
       <Button icon="sap-icon://nav-back" press="voltarMenu"/> 
      </contentLeft> 
      <contentMiddle> 
       <Label text="{i18n>newTicket}"/> 
      </contentMiddle> 
     </Bar> 
    </customHeader> 
    <footer> 
     <Bar> 
      <contentMiddle> 
       <Button id="btnSend" text="{i18n>send}" press="enviar" icon="sap-icon://add-activity-2"/> 
      </contentMiddle> 
     </Bar> 
    </footer> 
</Page> 

+0

안녕하세요 @mayconbelfort 다음 "만들기"방법

 oModelRequest.setHeaders({ "Access-Control-Allow-Origin" : "*", "Content-Type": "application/x-www-form-urlencoded", "X-CSRF-Token":"Fetch" }); // Declare a variable to handle the security token var token; // Create a read request to retreive the X-CSRF token oModelRequest.read('/Brand', null, null, false, function(oData, oResponse) { if (oResponse.headers['x-csrf-token'] == undefined) { //alert("Error on read process. No token ! Check read !"); } token = oResponse.headers['x-csrf-token']; }, function() { alert(oModeli18n.getProperty("Brand_token_error")); } ); 

그리고 그 후 나는 실제 "POST"수행에 "포스트"이벤트에 대한

, 나는 토큰 fech을 사용 , JSON 모델 [loadData] (https://sapui5.netweaver.ondemand.com/sdk/docs/api/symbols/sap.ui.model.json.JSONModel.html) 함수를 사용하여 유형을 ' POST ' –

답변

1

.

 var user = applicationContext.registrationContext.user; 
     var password = applicationContext.registrationContext.password; 
     var uri = "http://" + user + ":" + password + "@" + applicationContext.registrationContext.serverHost + ":8080/" + appId + "/" 
     var headers = { 
       //"Authorization" : "Basic " + btoa(applicationContext.registrationContext.user + ":" + applicationContext.registrationContext.password), 
       "X-SMP-APPCID" : applicationContext.applicationConnectionId 
      }; 

     console.log("Try comunicating the first time"); 
     var oModel = new sap.ui.model.odata.ODataModel(uri, {json : true}, user, password, headers, false, false, false); 

     oModel.setHeaders(headers); 
     oModel.read("/Brand", onSuccess); 

       function onSuccess(result) { 
        sap.ui.getCore()....getView().getModel("Brands").setData(result); 
        }; 

내 요청, 수동 또는 자동 (수동 이벤트 또는 페이지 이벤트)은 어떻게 수행하나요?

 // Set POST request header using the X-CSRF token 
     oModelRequest.setHeaders({ 
      "X-Requested-With": "XMLHttpRequest", 
      "Content-Type": "application/json", 
      "DataServiceVersion": "2.0", 
      "Accept": "application/atom+xml,application/atomsvc+xml,application/xml", 
      "X-CSRF-Token": token 
      }); 

     // Call the create request 

     oModelRequest.create('/Brand', requestData, null, 
        function(oData, oResponse) { 
        alert (Success);}, 
        function(oData) {      
        alert(Error)); 
        alert(oData.response.body);} 
        ); 
+0

어떻게이 정보를 저장합니까? 'var user = applicationContext.registrationContext.user;' 'var password = applicationContext.registrationContext.password; ' – mayconbelfort

+0

늦게 답장을 드려 죄송합니다. 가 나는 코르도바 로그온 플러그인을 사용하고있어 후 : // 로그온 구성 요소를 초기화 \t \t sap.Logon.init (logonSuccessCallback, errorCallback, APPID, 문맥, sap.logon.IabUi); 은 "logonSuccessCallback"fucntion에서 내가 가진 : \t 기능 logonSuccessCallback (결과) { 결과가 null가 아닌 경우 \t \t // 우리가 성공적으로 로그인 \t \t 경우 (결과) { \t \t \t의 ApplicationContext = 결과를 ; \t \t \t console.log ("First logonSuccess"); \t \t \t globalCoreModel.init(); jQuery.sap.require ("Application"); var oApp = 새 응용 프로그램 ({ \t 루트 : "콘텐츠" }); \t \t} \t} – Nepriceputu

관련 문제