Activity Forums Salesforce® Discussions How to use standard Salesforce REST API from Salesforce Visualforce Page?

  • shariq

    Member
    September 22, 2018 at 5:05 pm

    Befor any Ajax call, make sure to add ‘Bearer’ token in header. If using JQuery use below code snippet.

    $.ajax({
    type: reqType,
    beforeSend: function (xhr)
    {
    xhr.setRequestHeader("Authorization", 'Bearer {!$API.Session_ID}');

    },
    headers : {'Content-Type' : 'application/json; charset=utf-8'},
    url: postUrl,
    data: postData,
    dataType: 'text'
    })
    .done(function( data ) {
    //Code if success
    })
    .fail(function(xhr,textstatus,error){
    //Code if fail
    });

  • Parul

    Member
    September 22, 2018 at 6:10 pm

     

    I was able to call a REST API from the javascript on a VF page.

    function CallRestAPI()

    {

    var weblink = "http://URL";
    $.ajax(

    {

    url : weblink,

    type : 'GET',

    dataType: 'json',
    beforeSend: function (request)

    {

    request.setRequestHeader("HeaderName", "HeaderValue");

    },

    crossDomain: true,

    success : function(result)

    {

    //process the result

    },

    error : function(jqXHR, textStatus, errorThrown) {

    alert('Error: '+jqXHR.status);

    alert('ErrorThrown: '+errorThrown)

    }
    });
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos