0

I have a task to send the user's personal data with API. But I have to implement it with SOAP. Below in the code I am using simple jQuery + Ajax dispatch, But I am getting error

Access to XMLHttpRequest at 'https://send.data.to.api/' from origin 'https://website.url' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

If I understand correctly, the server side is blocking my host from which trying to send data and the server side should give access host. If not, how can the error be corrected?

jQuery(document).ready(function($){


    $('#send-candidate-data').on('click', function(){

    var personal_data = {
        'refCode': ref_code,
        'name': firstName,
        'lastname': lastName,
        'phone': phone,
        'email': email,
        'comment': comment,
        'file': newFiles,
        'gender': selectedGender
    }


    var soapApiUrl = 'https://send.data.to.api/' + ref_code + '?=' + job_portal;

    var payload  = '<?xml version="1.0" encoding="utf-8"?>' +
    '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
    '<soap:Body>' +
        '<JobApplication xmlns="http://www.host.net/">' +
            '<item>' +
                '<RefCode>' + personal_data.refCode + '</RefCode>' +
                '<SalutationGender>' + personal_data.gender +'</SalutationGender>' +
                '<FirstName>' + personal_data.name +'</FirstName>' +
                '<LastName>' + personal_data.lastname +'</LastName>' +
                '<EmailAddress>' + personal_data.email +'</EmailAddress>' +
                '<Phone>' + personal_data.phone + '</Phone>' +
                '<ApplicantComments>' + personal_data.commet +'</ApplicantComments>' +
            '</item>' +
        '</JobApplication>' +
    '</soap:Body>' +
    '</soap:Envelope>'; 


    var settings = {
        "async": true,
        "crossDomain": true,
        "url": soapApiUrl,
        "method": "POST",
        "headers": {
            'POST': '/services/website.asmx HTTP/1.1',
            'Host': 'host.job.io',
            'Content-Type': 'text/xml; charset=utf-8',
            'Content-Length': 'length',
            'SOAPAction': 'http://www.host.net/AboutJobApplication',
            'Access-Control-Allow-Origin': 'https://website.url',
            "cache-control": "no-cache"
        },
        "processData": false,
        "data": payload,

    }

    $.ajax(settings).done(function(response) {
        conosole.log(response);
    });


    })


});
stepbystep
  • 371
  • 1
  • 7
  • 21
  • Sounds like you need to do this outside of a browser, or else use a proxy (I do not recommend using a proxy unless you host it yourself, at which point you may aswell send the payload directly using something like node.js). – Abe Petrillo Sep 21 '20 at 18:06
  • @AbePetrillo I didn't understand a little, is the problem on the server side or in my code? – stepbystep Sep 21 '20 at 18:07
  • It's neither. When using Javascript in the browser, the server has told the browser not to send requests from any domains the server doesn't know about. The only way around this is to send the request outside of a browser, on your own server. – Abe Petrillo Sep 21 '20 at 18:22
  • @AbePetrillo Thanks for your reply, what if I send via php? – stepbystep Sep 21 '20 at 18:24
  • That would work. – Abe Petrillo Sep 22 '20 at 19:52

0 Answers0