In this blog we will learn how to send post request through AJAX in Visualforce page using jquery. Let’s get started.

POST Request through AJAX in Visualforce

The code is mentioned below:

<apex:page >
  <!-- 
        /**
         * Webkul Software.
         *
         * @category  Webkul
         * @author    Webkul
         * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
         * @license   https://store.webkul.com/license.html
         */
     -->  
  <apex:includeScript value="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"/>
  <script>
    jQuery(document).ready(function($) {
      // Get Accounts via the REST API
      $.ajax('/services/data/v41.0/sobjects/Account',{
          type:'POST',
          data:'{"Name":"Test Company"}',
          dataType: 'json',
          beforeSend: function(xhr) {
            // Set the OAuth header from the session ID
            xhr.setRequestHeader('Authorization', 'Bearer {!$Api.Session_ID}');            
            xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
            
          },
          success: function(response) {
            // Sit the result to HTML div          
              $('[Id$=accountId]').html(JSON.stringify(response));
          },
          error: function(jqXHR, textStatus, errorThrown) {
            // Error
            alert(jqXHR.status + ': ' + errorThrown);
          }
        }
      );
      
    });
    
  </script>
  <h1>REST API (POST method)</h1>
  <div>Account Info:</div>
  <div id="accountId">
  </div>
</apex:page>

Output

post request through ajax

Support

That’s all for how to send Post Request through AJAX in Visualforce page using jquery, still have any issue feel free to add a ticket and let us know your views to make the code better https://webkul.uvdesk.com/en/customer/create-ticket/

 

Leave A Reply

Please verify that you are not a robot.

Tell us about Your Company

How can we help you with your business?




    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home