0

Hello im trying to ajax cross domain call , but there is problem with headers. I followed many of tutorials but i just cant get it working.

This is how far did i go :

<html>
<head>
<meta http-equiv="Access-Control-Allow-Origin" content="*">
</head>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" ></script>

<script>

    jQuery.support.cors = true;

    $(document).ready(function() {

        //this would be done in a common script file if you are going to
        //make a lot of these calls
        $.ajaxSetup({ type: 'POST', dataType: 'json', contentType: 'application/json', data: {} });

        $('#btn').click(function() {
             //call the web service with the button is clicked
            $.ajax({ url: 'url_external',
                data: '{ "some_data:data" }', //ensure the data is enclosed in a quote
                            success: function (data) {
                alert('here...success');
            },
            complete: function (data) {
                alert('here');
            }
            });

            //return false to avoid a postback
            return false;
        });

    });
</script>


<div>
    <button id='btn'>Load It</button>
    <div id='sayit'></div>
</div>
</body>
</html>

And this only work in IE9. Am I missing something? In Mozilla Firefox and Google Chrome there is problem with options and 405 Method not allowed

Raptor
  • 53,206
  • 45
  • 230
  • 366
Martin Fric
  • 726
  • 2
  • 9
  • 27

1 Answers1

0

Add this to all of your HTTP responses on the 'url_external' server:

<?php

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers', 'Content-Type');

This enables CORS Ajax requests on your url_external server.

You'd probably want to fine tuned this to...

header('Access-Control-Allow-Origin: http://yourdomain.com');

... in order to allow only CORS requests coming only from your domain.

The best information I found about CORS: Using CORS.

eightyfive
  • 4,601
  • 3
  • 35
  • 44