1

enter image description hereXMLHttpRequest cannot load https://login.salesforce.com/services/Soap/u/33.0. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access

$( document ).ready(function() {

        var param = '<?xml version="1.0" encoding="utf-8" ?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"><env:Body><n1:login xmlns:n1="urn:partner.soap.sforce.com"><n1:username>{username}</n1:username><n1:password>{password}{access_token}</n1:password></n1:login></env:Body></env:Envelope>';

        $.ajax({
            url: 'https://login.salesforce.com/services/Soap/u/33.0',
            type: 'POST',
            data: param,
            dataType: "xml",
            contentType: "application/x-www-form-urlencoded",
            crossDomain : true,
            beforeSend: function(xhr) {
                xhr.setRequestHeader('SOAPAction', 'login'),
                xhr.setRequestHeader('Content-Type', 'text/xml; application/x-www-form-urlencoded;charset=UTF-8'),
                xhr.setRequestHeader('Content-Description', 'SOAP')
            },
            success: function (data) {
                console.log(data);
                $('#divajax').html(xml_to_string(data));
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
                console.log(xhr.responseText);
                $('#divajaxerr').html(xhr.responseText);
              }
        });

    });
0

I think you are missing few steps before calling REST API

  1. You will need to setup and pass oAuth 2.0 properly. https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_user_agent_oauth_flow.htm
  2. You will need to setup whitelist address in CORS https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/extend_code_cors.htm
  • 1
    1. Yes. I want to get access token using username , password & access token only.There is a way to get access token using SOAP API.I need that way.If I put these details into Chrome Postman Plugin It works well.What i wanted to do is i need to pass ajax request and get access token. 2. How i enable all cors (*) in Salesforce ? – Kavinda Wickramasingha May 29 '17 at 7:02

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.