0

I m making ajax call like this :

            var username = "someusername";
            var password= "somepassword";

    var datam = '{"description":"my description","account":"mehame","username":"hakan","password":"11"}';

    $.ajax
            ({
                url: "http://mywebsite.com:8080/directory/send",
                contentType: "application/json",
                data: datam,
                dataType: 'text',
                type: "PUT",
                headers: {
                    "Authorization": btoa(username + ":" + password)
                  },
                beforeSend: function (){
                    //xhr.setRequestHeader('Authorization', make_base_auth(username, password));
                    console.log(datam);
                },
                success: function (){
                    alert("Here's lots of data, just a string: " + datam);
                },
                error : function(){
                    alert("Here's lots of data, just a string: " + datam);
                }
            });

        });

And firebug console gave me "Cross Domain Error" (and returned error) :

"Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://mywebsite.com:8080/directory/send. This can be fixed by moving the resource to the same domain or enabling CORS."

But in my chrome extensions name is "PostMan" its completely working.

What is different?

mehmetakifalp
  • 445
  • 5
  • 16

1 Answers1

0

Due to same-origin policy, you can't execute ajax request on a foreign hostname or port number. http://en.wikipedia.org/wiki/Same-origin_policy

You must enable CORS in your http headers : http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

You can also see this post to know how to do that : cant get CORS working in chrome and firefox

Community
  • 1
  • 1
Rey0bs
  • 1,192
  • 12
  • 19