How to call web API with GET method basic authentication into jquery() in javascript












0















Hello I am using this method to read this api it giving CORS error. I have added CORS plugin with chrome then also it is not coming. Please let me know how to solve these to error.



text:



 function NoCors() {
debugger;
var uName = "*****";
var passwrd = "*****";
document.write("Enter1");
var url = 'http://219.90.67.163:8207/api/Info/getgismeterdata'
$.ajax({
type: 'GET',
url: url,
crossDomain: true,
//Access-Control-Allow-Credentials: true,
contentType: 'json',
datatype: "application/json",
headers: {
"Authorization": "Basic QXBpVXNlcjpBcGlQYXNz",
},

beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic " + btoa(uName + ":" + passwrd));
},
success: function (data) {
debugger;
console.log("data")
//Success block
},
error: function (xhr, ajaxOptions, throwError) {
//Error block
},
});
}


error in console:
1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
2. Access to XMLHttpRequest at 'http://219.90.67.163:8207/api/Info/getgismeterdata' from origin 'http://localhost:50362' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.










share|improve this question























  • have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

    – Abid Hussain
    Nov 15 '18 at 8:48













  • contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

    – Quentin
    Nov 15 '18 at 9:38











  • datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

    – Quentin
    Nov 15 '18 at 9:38











  • You have headers and xhr.setRequestHeader: Pick one!

    – Quentin
    Nov 15 '18 at 9:41











  • crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

    – Quentin
    Nov 15 '18 at 9:42
















0















Hello I am using this method to read this api it giving CORS error. I have added CORS plugin with chrome then also it is not coming. Please let me know how to solve these to error.



text:



 function NoCors() {
debugger;
var uName = "*****";
var passwrd = "*****";
document.write("Enter1");
var url = 'http://219.90.67.163:8207/api/Info/getgismeterdata'
$.ajax({
type: 'GET',
url: url,
crossDomain: true,
//Access-Control-Allow-Credentials: true,
contentType: 'json',
datatype: "application/json",
headers: {
"Authorization": "Basic QXBpVXNlcjpBcGlQYXNz",
},

beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic " + btoa(uName + ":" + passwrd));
},
success: function (data) {
debugger;
console.log("data")
//Success block
},
error: function (xhr, ajaxOptions, throwError) {
//Error block
},
});
}


error in console:
1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
2. Access to XMLHttpRequest at 'http://219.90.67.163:8207/api/Info/getgismeterdata' from origin 'http://localhost:50362' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.










share|improve this question























  • have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

    – Abid Hussain
    Nov 15 '18 at 8:48













  • contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

    – Quentin
    Nov 15 '18 at 9:38











  • datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

    – Quentin
    Nov 15 '18 at 9:38











  • You have headers and xhr.setRequestHeader: Pick one!

    – Quentin
    Nov 15 '18 at 9:41











  • crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

    – Quentin
    Nov 15 '18 at 9:42














0












0








0








Hello I am using this method to read this api it giving CORS error. I have added CORS plugin with chrome then also it is not coming. Please let me know how to solve these to error.



text:



 function NoCors() {
debugger;
var uName = "*****";
var passwrd = "*****";
document.write("Enter1");
var url = 'http://219.90.67.163:8207/api/Info/getgismeterdata'
$.ajax({
type: 'GET',
url: url,
crossDomain: true,
//Access-Control-Allow-Credentials: true,
contentType: 'json',
datatype: "application/json",
headers: {
"Authorization": "Basic QXBpVXNlcjpBcGlQYXNz",
},

beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic " + btoa(uName + ":" + passwrd));
},
success: function (data) {
debugger;
console.log("data")
//Success block
},
error: function (xhr, ajaxOptions, throwError) {
//Error block
},
});
}


error in console:
1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
2. Access to XMLHttpRequest at 'http://219.90.67.163:8207/api/Info/getgismeterdata' from origin 'http://localhost:50362' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.










share|improve this question














Hello I am using this method to read this api it giving CORS error. I have added CORS plugin with chrome then also it is not coming. Please let me know how to solve these to error.



text:



 function NoCors() {
debugger;
var uName = "*****";
var passwrd = "*****";
document.write("Enter1");
var url = 'http://219.90.67.163:8207/api/Info/getgismeterdata'
$.ajax({
type: 'GET',
url: url,
crossDomain: true,
//Access-Control-Allow-Credentials: true,
contentType: 'json',
datatype: "application/json",
headers: {
"Authorization": "Basic QXBpVXNlcjpBcGlQYXNz",
},

beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', "Basic " + btoa(uName + ":" + passwrd));
},
success: function (data) {
debugger;
console.log("data")
//Success block
},
error: function (xhr, ajaxOptions, throwError) {
//Error block
},
});
}


error in console:
1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
2. Access to XMLHttpRequest at 'http://219.90.67.163:8207/api/Info/getgismeterdata' from origin 'http://localhost:50362' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.







javascript jquery html5 asp.net-web-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 8:35









Diksha BandilDiksha Bandil

1




1













  • have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

    – Abid Hussain
    Nov 15 '18 at 8:48













  • contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

    – Quentin
    Nov 15 '18 at 9:38











  • datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

    – Quentin
    Nov 15 '18 at 9:38











  • You have headers and xhr.setRequestHeader: Pick one!

    – Quentin
    Nov 15 '18 at 9:41











  • crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

    – Quentin
    Nov 15 '18 at 9:42



















  • have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

    – Abid Hussain
    Nov 15 '18 at 8:48













  • contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

    – Quentin
    Nov 15 '18 at 9:38











  • datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

    – Quentin
    Nov 15 '18 at 9:38











  • You have headers and xhr.setRequestHeader: Pick one!

    – Quentin
    Nov 15 '18 at 9:41











  • crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

    – Quentin
    Nov 15 '18 at 9:42

















have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

– Abid Hussain
Nov 15 '18 at 8:48







have a look at docs.microsoft.com/en-us/aspnet/web-api/overview/security/…

– Abid Hussain
Nov 15 '18 at 8:48















contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

– Quentin
Nov 15 '18 at 9:38





contentType: 'json', — That is not a valid value for contentType which you shouldn't be setting at all because you are making a GET request.

– Quentin
Nov 15 '18 at 9:38













datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

– Quentin
Nov 15 '18 at 9:38





datatype: "application/json", — The property is called dataType with a capital T and application/json is not a valid value for it. You probably mean "json".

– Quentin
Nov 15 '18 at 9:38













You have headers and xhr.setRequestHeader: Pick one!

– Quentin
Nov 15 '18 at 9:41





You have headers and xhr.setRequestHeader: Pick one!

– Quentin
Nov 15 '18 at 9:41













crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

– Quentin
Nov 15 '18 at 9:42





crossDomain: true — This is pointless unless you are making a same origin request which gets redirected to a different origin.

– Quentin
Nov 15 '18 at 9:42












3 Answers
3






active

oldest

votes


















0














Before sending the GET request to server, browser automatically send and preflight OPTIONS request, and your server doesn't allow this method. You need to enable OPTIONS method support in your server side (219.90.67.163)






share|improve this answer































    0
















    1. Are you use Bearer Token Authentication?
      requst headers try this



      headers: {
      "Authorization": "Bearer QXBpVXNlcjpBcGlQYXNz",
      },



    2. You must setting CORS in Web API



    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api






    share|improve this answer































      0














      The CORS plugin you are using only works for Simple Requests.



      Since you are setting an Authorization header you are making a Preflighted Request which your plugin cannot handle.



      You need a different approach to handling the Same Origin Policy. Ideally, that would be proper support for CORS on the server you are making the HTTP request to but some other options are listed at the end of this answer.






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315274%2fhow-to-call-web-api-with-get-method-basic-authentication-into-jquery-in-javasc%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Before sending the GET request to server, browser automatically send and preflight OPTIONS request, and your server doesn't allow this method. You need to enable OPTIONS method support in your server side (219.90.67.163)






        share|improve this answer




























          0














          Before sending the GET request to server, browser automatically send and preflight OPTIONS request, and your server doesn't allow this method. You need to enable OPTIONS method support in your server side (219.90.67.163)






          share|improve this answer


























            0












            0








            0







            Before sending the GET request to server, browser automatically send and preflight OPTIONS request, and your server doesn't allow this method. You need to enable OPTIONS method support in your server side (219.90.67.163)






            share|improve this answer













            Before sending the GET request to server, browser automatically send and preflight OPTIONS request, and your server doesn't allow this method. You need to enable OPTIONS method support in your server side (219.90.67.163)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 15 '18 at 9:02









            Quy NguyenQuy Nguyen

            589




            589

























                0
















                1. Are you use Bearer Token Authentication?
                  requst headers try this



                  headers: {
                  "Authorization": "Bearer QXBpVXNlcjpBcGlQYXNz",
                  },



                2. You must setting CORS in Web API



                https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api






                share|improve this answer




























                  0
















                  1. Are you use Bearer Token Authentication?
                    requst headers try this



                    headers: {
                    "Authorization": "Bearer QXBpVXNlcjpBcGlQYXNz",
                    },



                  2. You must setting CORS in Web API



                  https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api






                  share|improve this answer


























                    0












                    0








                    0









                    1. Are you use Bearer Token Authentication?
                      requst headers try this



                      headers: {
                      "Authorization": "Bearer QXBpVXNlcjpBcGlQYXNz",
                      },



                    2. You must setting CORS in Web API



                    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api






                    share|improve this answer















                    1. Are you use Bearer Token Authentication?
                      requst headers try this



                      headers: {
                      "Authorization": "Bearer QXBpVXNlcjpBcGlQYXNz",
                      },



                    2. You must setting CORS in Web API



                    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 15 '18 at 9:33









                    Homura LinHomura Lin

                    10116




                    10116























                        0














                        The CORS plugin you are using only works for Simple Requests.



                        Since you are setting an Authorization header you are making a Preflighted Request which your plugin cannot handle.



                        You need a different approach to handling the Same Origin Policy. Ideally, that would be proper support for CORS on the server you are making the HTTP request to but some other options are listed at the end of this answer.






                        share|improve this answer




























                          0














                          The CORS plugin you are using only works for Simple Requests.



                          Since you are setting an Authorization header you are making a Preflighted Request which your plugin cannot handle.



                          You need a different approach to handling the Same Origin Policy. Ideally, that would be proper support for CORS on the server you are making the HTTP request to but some other options are listed at the end of this answer.






                          share|improve this answer


























                            0












                            0








                            0







                            The CORS plugin you are using only works for Simple Requests.



                            Since you are setting an Authorization header you are making a Preflighted Request which your plugin cannot handle.



                            You need a different approach to handling the Same Origin Policy. Ideally, that would be proper support for CORS on the server you are making the HTTP request to but some other options are listed at the end of this answer.






                            share|improve this answer













                            The CORS plugin you are using only works for Simple Requests.



                            Since you are setting an Authorization header you are making a Preflighted Request which your plugin cannot handle.



                            You need a different approach to handling the Same Origin Policy. Ideally, that would be proper support for CORS on the server you are making the HTTP request to but some other options are listed at the end of this answer.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 15 '18 at 9:40









                            QuentinQuentin

                            650k728831044




                            650k728831044






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53315274%2fhow-to-call-web-api-with-get-method-basic-authentication-into-jquery-in-javasc%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python