Jquery.ajax () GET send request OPTIONS

I am trying to send an ajax request from my localhost development window to the server using

<!DOCTYPE html>
<html>
<head>
    <title>JSSample</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
    $(function() {
        var params = {
            format: "JSON",
            season: "2015REG"
            // Request parameters
        };

        $.ajax({
            url: "https://<mysite>/sub/" + params.format + "/" + params.season,
            beforeSend: function(xhrObj){
                // Request headers
                xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<mykey>");
            },
            type: "GET"
        })
        .done(function(data) {
            alert("success");
            console.log("Data: \n" + JSON.stringify(data));
        })
        .fail(function(error) {
            alert("error");
            console.log("Error" + JSON.stringify(error));
        });
    });
</script>
</body>
</html>

when I look in firefox, I see that it actually sends an OPTIONS request - to which the server is not responding (404 not found), so I get an error with a request for cross-request.

OPTIONS 
XHR 
https://mysite [HTTP/1.1 404 Resource Not Found 2437ms]
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://mysite (Reason: CORS header 'Access-Control-Allow-Origin' missing).

What am I doing wrong? Why is jquery trying to set parameters when I explicitly send a GET? Also, I do not own this server, its another company that has a subscription API, so I cannot provide a key or anything else to help troubleshoot the problems.

+4
source share
2 answers

, cors

( *) ajax

JavaScript - , , . JavaScript .

CORS , . , ( ). CORS -, API .


CORS whateverorigin.org, .

 $.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('http://google.com') + '&callback=?', function(data){
    alert(data.contents);
});
+1

( ) CORS ,

  • GET, HEAD POST;

, CORS . , , , , CORS

, , , , () , "" , .

+1

Source: https://habr.com/ru/post/1611526/


All Articles