5

I am trying to send a post request with the following code. But the request goes as GET request, instead of POST. How to fix this.

$.ajax({
    url: 'https://www.exampleurl.com',
    method: 'POST',
    headers: {"Access-Control-Allow-Origin": true},
    data: {url:'bla',call:"trans"}
    dataType: 'jsonp',
    success: function(data){
      console.log('succes: '+data);
    }
  });

This is the error I am getting XMLHttpRequest cannot load https://example.com. 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. The response had HTTP status code 401.

When removed the header Access-Control-Allow-Origin, I am getting a 404 error

gates
  • 4,465
  • 7
  • 32
  • 60

2 Answers2

2

I don't think, you can use a POST method with jsonp request. jsonp callbacks only for with GET method. Have a look at link .

Community
  • 1
  • 1
Sudipta Mondal
  • 2,550
  • 1
  • 19
  • 20
0

You don't have to pass parameters in url attribute when you want to send POST request you should use data attribute instead, take a look at jQuery.ajax() :

$.ajax({
    url: 'https://www.exampleurl.com',
    method: 'POST',
    data: {q:1, q2:2},
    headers: {"Access-Control-Allow-Origin": true},
    dataType: 'jsonp',
    success: function(data){
      console.log('succes: '+data);
    }
});

Hope this helps.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101