Access-Control-Allow-Origin problem with $.ajax call

Hi all,
I have a problem when I try to call an api from frappe via jquery ajax!
the api is called correctly but I can not receive the return message from it and i have a Access-Control-Allow-Origin error

how can i solve it plz?
Thanks
Maysaa

Can you post your code?

1 Like

@Ben_Cornwell_Mott Here the code:

$.ajax({
url: “{{url(‘http://x.x.x.x/api/method/erpstyle.myapi.install_site’)}}”,

        type: "POST",
        data: post,
        dataType: "json",


        success: function(data) {
           alert("done");
        return document.location.href = "/"+data.message;
           var b = "http://"+data.message;
        return alert('<a>'+b+'</a>');
        window.open(b, '_blank');

        console.log(data);

    },
    error: function() {
       alert('Error occured');
    }
   });

Check this one,

$.ajax({
	url: "http://x.x.x.x/api/method/erpstyle.myapi.install_site",
	type: 'POST',
	dataType: 'json',
	success: function (data, textStatus, xhr) {
		console.log(data)
		msgprint(__("Success!!"));
	},
	error: function (data, textStatus, xhr) {
		msgprint(__("Failure!!"));
	}
});

@Sangram The same error :frowning:

No issue in ajax call . It is working fine. Check your install_site as it is executing error block.[quote=“Sangram, post:4, topic:24615”]
error: function (data, textStatus, xhr) {
msgprint(__(“Failure!!”));
}
[/quote]

You shouldn’t be using ajax for those calls. Use frappe.call instead (search github or the forum for examples)

Thanks @Ben_Cornwell_Mott fro your reply.
OK I know that but I call it from apache server

The reason you are getting this error is because you are calling the ajax command from a different website than the one the browser is currently at. All browsers have a security feature that prevents this from happening. The reason is that otherwise, it would be very easy for a website to use your browser to DDOS another website.

There are ways to disable this security feature in your browser, but that won’t work for other users and is typically very dangerous.

2 Likes