11

I use jquery.ajax function to post data from google chrome extension to my web service as code below:

$.ajax({
            type: "POST",
            url: serviceUrl,
            data: data,
            success: function(msg){
                if(typeof(Me.config.onSumitted) == "function"){
                    Me.config.onSumitted(msg);
                }
           },
           error: function(){
                if(typeof(Me.config.onError) == "function"){
                    Me.config.onError();
                }
           }
         });

but i get an error:

XMLHttpRequest cannot load http://todomain.com/Service.asp. Origin http://fromtabdomain.com is not allowed by Access-Control-Allow-Origin.

how can i resolve it?

5 Answers 5

14

you need to add a permission to you manifest.js file

"permissions": [
    "http://www.yourwebsite.com/"
  ],
Sign up to request clarification or add additional context in comments.

1 Comment

Make sure to include the trailing slash, otherwise it won't work.
13

You can have a look at this page to achieve what you want :

http://code.google.com/chrome/extensions/xhr.html

It is just about setting permissions...

2 Comments

this is not working for me, any idea why? my ajax call is successful, badge is updated but notification is not working.
Please don't post "link only" answers. This link is no longer valid. developer.chrome.com/docs/extensions/develop/concepts/… might be the correct one, but I'm not sure enough of that to edit your answer.
3

As @ChristopheCVB pointed out http://code.google.com/chrome/extensions/xhr.html tells you what to do:

Please add a permissions section to your manifest.json:

{
  "name": "yourExtension",

  "permissions": [
    "http://fromtabdomain.com"
  ]
}

Comments

1

its because same origin policy set crossDomain to true (ise jquery version 1.5 or higher)

$.ajax({
            type: "POST", //or GET
            url: serviceUrl,
            data: data,
            crossDomain:true,
            cache:false,
            async:false,
            success: function(msg){
                //do some thing
           },
           error: function(jxhr){
               alert(jxhr.responseText);
                //do some thing
           }
         });

1 Comment

Thanks for your answer, but i want to post data to my service.
1

I use native ajax to solve this problem.you can do that with following steps.

ajax = function(options, callback) {
  var xhr;
  xhr = new XMLHttpRequest();
  xhr.open(options.type, options.url, options.async || true);
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      return callback(xhr.responseText);
    }
  };
  return xhr.send();
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.