0

In my php code I am using following Ajax request. The Ajax request showing following error in Chrome browser console

XMLHttpRequest cannot load http://example.com/posts. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. (index):

My code :

<script>
    $(document).ready(function() {
        var title = 'Tes ajax';
        var raw = 'very good';

        //Validate fields if required using jQuery

        var postForm = { //Fetch form data
            'api_key'     : 'bf_apikey',
            'api_username': 'username',
            'title'       : title,
            'raw'         : raw,
            'category'    :'categoryname'//Store name fields value
        };

        $.ajax({ //Process the form using $.ajax()
            type      : 'POST', //Method type
            url       : 'http://example.com/posts', //Your form processing file URL
            data      : postForm,           //Forms name
            dataType  : 'application/json',
            success:  updatesuccess,
            error: updatefailure
            // return response; // <- tried that one as well
        });
        function updatesuccess(result)
        {
            alert(result+"Successs")
        }

        function updatefailure()
        {
            alert("failure")
        }          

    });
</script>

please suggest better option to solve this error.

captainsac
  • 2,484
  • 3
  • 27
  • 48
Team
  • 347
  • 1
  • 5
  • 16
  • http://stackoverflow.com/questions/10143093/origin-is-not-allowed-by-access-control-allow-origin – Izion May 29 '15 at 08:19

1 Answers1

0

CORS problem mate. Three possible solutions:

1) JSONP;

2) Create a proxy on the same domain, send your request to the proxy and use the proxy to call the service;

3) If you have access to "example.com" (the source of where you're making the ajax call to) then add the following header: header('Access-Control-Allow-Origin: *');

Evernoob
  • 5,551
  • 8
  • 37
  • 49
  • I tried with your first solution.That's not working i am still getting the same error.And also i tried the third solution.but i have one doubt where need to add header('Access-Control-Allow-Origin: *'); – Team May 29 '15 at 08:46