-3

I am trying to call an API that accepts OPTIONS method on server, it runs on postman and returns json object but following code is not working on js

I have read that OPTIONS call is a preflight call for CORS calls, so this API is https and on another server. But even then there is no response and it returns 405 method not found

 $.ajax({
            url: url,
            dataType: "jsonp",
            method :"OPTIONS",
            crossDomain: true,
            contentType: 'application/json',
            headers: {
                "Content-type": "application/json",
                "Cache-Control": "no-cache",
                "Accept": "application/json,long_format",
                "Access-Control-Allow-Origin": "*"
            },
            success: function (data) {
                console.log("success" + data);
            },
            error: function (data) {
                console.log("fail" + data);
            }
        }).fail(function(data) {
            console.log("failed" + data);
            });

Extra info : The API is cross domain and on ssl so to cover cross domain request I had to user dataType: "jsonp"

UPDATED :

This is impossible scenario so I have to get update on server end...

Explanation: There is some problem with OPTIONS method that is behind cross domain as well a/c to some research i have done on internet, CORS request can be accessed with : dataType: "jsonp", but with -> dataType: "jsonp" you can only call GET methods so we are stuck here that allows that either we call cross domain https request or we can call OPTIONS method, usually OPTIONS method is a preflight method done automatically by browser

NOW please stop down voting my question

Shahrukh Ahmad
  • 133
  • 1
  • 1
  • 10

1 Answers1

2
dataType: "jsonp",

Take this out. JSONP requests are always GET requests. This is your main problem.

crossDomain: true,

Take this out. It does nothing unless you are making a same origin request that gets redirected to be a cross origin request.

contentType: 'application/json',

Take this out. You are making an OPTIONS request. There is no request body to describe the content-type of.

"Content-type": "application/json",

Take this out. For the same reason.

"Access-Control-Allow-Origin": "*"

Take this out. It is a response header and has no place on the request.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • the request is is cross origin and https method and without it `dataType: "jsonp` it gives cross origin policy issue – Shahrukh Ahmad Jul 31 '19 at 17:58
  • @ShahrukhAhmad — Then you need to change the server to use CORS to grant you permission to make the request. JSONP can **only** be made with GET requests. – Quentin Jul 31 '19 at 18:00
  • you mean it can't be made manually? I have called this OPTIONS method from postman and it returns proper result.. There should be some way to call it from ajax too – Shahrukh Ahmad Jul 31 '19 at 18:04
  • 1
    Go and read https://stackoverflow.com/a/35553666/19068 which explains the point of the same-origin policy. – Quentin Jul 31 '19 at 18:05