jQuery.ajax call fails inside Chrome extension

前端 未结 4 1537
时光说笑
时光说笑 2020-12-28 21:56

I\'m porting one of my Firefox extensions to Chrome, and I\'m running into a little problem with an AJAX query. The following code works fine in the FF extension, but fails

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-28 22:38

    Check your manifest file. Does the extension have permission to access that url?

    If it helps to your second problem (or anyone else): You can send a request to your background page like:

    chrome.extension.sendRequest({var1: "var1value", var2: "value", etc}, 
     function(response) {
        //Do something once the request is done.
    }); 
    

    The Variable response can be anything you want it to be. It can simply be a success or deny string. Up to you.

    On your background page you can add a listener:

    chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
          // Do something here
          // Once done you can send back all the info via:
          sendResponse( anything you want here );
    
          // and it'll be passed back to your content script.
    });
    

    With this you can pass the response from your AJAX request back to your content script and do whatever you wanted to do with it there.

提交回复
热议问题