0

I try to make bootstrap table with server-side pagination. That's why I created this REST service:

@RestResource(urlMapping='/Test/*')
global with sharing class Test{

 @HttpGet
 global static String doGet() {
    Room r = new Room();
    r.id = 0;
    r.name = 'Item 0';
    r.price= '$0';

    List<Room> r1 = new List<Room>();
    r1.add(r);

    return JSON.serialize(r1);
}


public class Room{
    public Integer id{get;set;}
    public String name{get;set;}
    public String price{get;set;}
}
}

then I'm trying to consume this rest in bootstrap table

<table id="table"
           data-toggle="table"
           data-url="/services/apexrest/Test/"
           data-height="400"
           data-side-pagination="server"
           data-pagination="true"
           data-page-list="[5, 10, 20, 50, 100, 200]"
           data-search="true">
        <thead>
        <tr>
            <th data-field="state" data-checkbox="true"></th>
            <th data-field="id">ID</th>
            <th data-field="name">Item Name</th>
            <th data-field="price">Item Price</th>
        </tr>
        </thead>
    </table>

Unfortunately, when I try to display the data, I get an error (401 UNAUTHORIZED). So how can I authenticate? I do all queries within one organization. Unfortunately, I browsed the internet, but can't find the solution.

2

You need to include the Authorization header. You haven't specified exactly how your module works, but you basically need to set the Authorization header to the following value:

var authHeader = "OAuth {!$Api.Session_ID}";
req.setRequestHeader("Authorization", authHeader);
  • look on m answer – Adamo Nov 8 '16 at 9:14
0

I try to consume REST according to your advice:

  $(function() {
         var authHeader = "OAuth {!$Api.Session_ID}";
         sforce.connection.init('{!$Api.Session_ID}', '/services/apexrest/Test/'); 


        sforce.connection.remoteFunction({
            url : "/services/apexrest/Test/",
            requestHeaders: {
                "Authorization": authHeader,
                "Content-Type":"application/json", 
                "Connection":"Keep-Alive" 
            },
            method: "GET",
            dataType: "jsonp",
            onSuccess : function(response) {
                  console.log("Success " +response);  
               },
           onFailure : function(response) {
                  console.log("Failed " + response)
              }
       });    
       });

but console show me that error:

Failed Error 400 Unable to forward request due to: no protocol: /services/apexrest/Test/

HTTP ERROR 400

Problem accessing /services/proxy. Reason:

    Unable to forward request due to: no
protocol: /services/apexrest/Test/


Powered by Jetty://

  • 1
    your URL should be "https : //cs2.salesforce.com/services/apexrest/Test/" where cs2 denotes your insatnce url – Pranay Jaiswal Nov 8 '16 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.