Resolve CORS issue without changing services.yml file I have below in routing.yml

sample.post:
  path: '/sample/post'
  defaults:
    _controller: '\Drupal\sample\Controller\SampleController::postData'
  methods: [POST]
  requirements:
    _access: 'TRUE'
    _format: 'json'


In services.yml no change it will be same as default. I don't want to change in service.yml by enabling cors config.

When I call using jQuery.

  jQuery.ajax({
      url: 'http://localhost/sample/post',
      contentType: "application/json",
      type: 'POST',
      complete: function (data) {
        console.log(data);
      }
  });


It gives


  Failed to load http://localhost/sample/post: Response to preflight
  request doesn't pass access control check: No
  'Access-Control-Allow-Origin' header is present on the requested
  resource. Origin 'http://localhost' is therefore not allowed access.


Can I achieve w/o enabling cors config? If I enable cors then it works fine.
Can I achieve w/o enabling cors config?


No. Without CORS configured, the server and browser will correctly conspire to reject the request.

You could implement it yourself, rather than using services.yml, but something needs to set the headers. And at the point where all you need to do is make a few simple edits to a configuration file, I'd question why you would want to write all that extra code yourself.