0
<script type="application/javascript">
     $(function () {
         $('#btnUsingjQuery').click(function () {
             $.ajax({
                 url: "https://maps.googleapis.com/maps/api/js?key=12345",
                 dataType: 'jsonp',
                 success: function (results) {
                     window.location.href = "www.google.com";
                 }
             });
         });
     });
     </script>

above are my jquery code which connect to a external api,no matter success or fail it will redirect to google.com.

i faced error after click the button : Refused to execute script from API because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled. tried change type="application/javascript" still but facing the issue. any ideas why?

ichbinblau
  • 4,507
  • 5
  • 23
  • 36
KyLim
  • 468
  • 1
  • 6
  • 22

2 Answers2

0

If you set it to json, you will see error, which means that you don't have permission to access to that API. I think if you want to use Google Map Api, you need to register and get a token for accessing to the API.

XMLHttpRequest cannot load https://maps.googleapis.com/maps/api/js?key=12345. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="btnUsingjQuery">Click</button>

<script type="application/javascript">
     $(function () {
         $('#btnUsingjQuery').click(function () {
             $.ajax({
                 url: "https://maps.googleapis.com/maps/api/js?key=12345",
                 dataType: 'json',
                 success: function (results) {
                     window.location.href = "www.google.com";
                 }
             });
         });
     });
     </script>
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ,getting this error... – KyLim Dec 02 '16 at 07:01
0

you try this....

    <script type="application/javascript">
         $(function () {
             $('#btnUsingjQuery').click(function () {
                 $.ajax({
                     url: "https://maps.googleapis.com/maps/api/js?key=12345",
                     dataType: 'jsonp',
                     success: function (results) {
                         window.location='https://www.google.co.in';
                     }
                 });
             });
         });
    </script>

i mean just add

window.location='https://www.google.co.in';
Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
  • No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ,getting this error..... – KyLim Dec 02 '16 at 07:02
  • just check this link - http://stackoverflow.com/questions/28547288/no-access-control-allow-origin-header-is-present-on-the-requested-resource-err – Karthick Nagarajan Dec 02 '16 at 07:16