вторник, 12 декабря 2017 г.

How to call SAP BPM OData service api from jQuery ajax

How to call SAP BPM OData service api from jQuery ajax.

Call with "GET" method:
If you want to call BPM OData service via http "GET" method, then you just call it via jQuery $.ajax.

Call with "POST" method:
If you want to call BPM OData service via http "POST" method, then first you have to call some BPM OData service with "GET" method with http header 'x-csrf-token' with value 'Fetch' to get token. Then you have to call BPM OData service with "POST" method and you have to set 'x-csrf-token' header which you recieved in previous step.

Example:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<div class="MyTest">Hello</div>
<div class="result1">result1</div>
<div class="result2">result2</div>

<script type="text/javascript">

$.ajax({
    url: "http://<server>:<port>/bpmodata/processes.svc/ProcessCollection('<bpm process id>')?$format=json",
    headers: {     
        'x-csrf-token':'Fetch',     
    },
    method: 'GET',
    success: function(data, textStatus, request){
var myToken = request.getResponseHeader('x-csrf-token');                
        $(".result1").html(myToken);

        $.ajax({
            url: "http://<server>:<port>/bpmodata/processes.svc/Cancel?InstanceId='<bpm process id>'&$format=json",
            headers: {     
                'x-csrf-token': myToken,     
            },
            method: 'POST',
            success: function(data, textStatus, request){                
                $(".result2").html('completed');
           },
           error: function (request, textStatus, errorThrown) {
                alert('Error');
           } 
        });
     
   },
   error: function (request, textStatus, errorThrown) {
        alert('Error' + request.getResponseHeader('x-csrf-token'));
   }

 
  });

</script>

</body>
</html>

Комментариев нет:

Отправить комментарий