Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax from Chrome-Extension processed, but receive responseText="" and status=0

I am writing a google-chrome extension, that needs to make ajax requests to a server, send some data, and receive some data back. My server is Tomcat 6.0, running on localhost.

I am able to receive all the data on the server side, do all the processing I need, and send a response back to the extension, but the status i get in the callback is 0, and responseText="".
my guess is that the problem lies either in the server - returning a response to a request originating from chrome-extension://... url, or in the extension - receiving a response from localhost:8080.

I've set the necessary permissions of course, and I tried setting content-type of the response to "text/xml", "text/html" and "text/plain" - it makes no difference. I've tried using ajax both with XMLHttpRequest and JQuery - same problem with both. I've found these issues, but they don't seem to solve my problem:
1. http://www.plee.me/blog/2009/08/ajax-with-chrome-empty-responsetext/
2. http://bugs.jquery.com/ticket/7653

here's my code: bg.js (background page)

function saveText(data) {
var requrl = serverUrl + addTextUrl;
var params = json2urlParams(data);
jQuery.ajax({
        type : "POST", 
        url : requrl, 
        data : params, 
        success : function (data, textStatus, XMLHttpRequest) {
            console.log("Data Saved: " + msg);
        }
    });

// var xhr = new XMLHttpRequest();
// xhr.open("POST", requrl, true);
// xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
// xhr.onreadystatechange = function (progress) {
// if (xhr.readyState == 4) {
// console.log("Data Saved: " + this.response);
// }
// };
// xhr.send(params);

}

addContentServlet.java: (server side)

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    ErrorCodes error = addContent(request, response);
    response.setContentType("text/plain");
    //response.setContentType("application/x-www-form-urlencoded; charset=UTF-8");      
    //response.setIntHeader("errorCode", error.ordinal());
    response.getWriter().write(error.toString());
    response.setIntHeader("errorcode", error.ordinal());
    if(error == ErrorCodes.SUCCESS){
        response.setStatus(error.toHttpErrorCode());
        response.flushBuffer();
    }
    else{
        response.sendError(error.toHttpErrorCode(), error.toString());
    }           
}

EDIT:

I've noticed in the chrome console of the background page that for every ajax that returns to the extension i get a

XMLHttpRequest cannot load http:// localhost:8080/stp_poc/MyServlet. Origin chrome-extension://fmmolofppekcdickmdcjflhkbmpdomba is not allowed by Access-Control-Allow-Origin.

I tried loosing bg.js and puting all the code in the main page instead - to no avail. how come XMLHttpRequest agrees to send the request, but not receive it back?? Maybe a server-configuration problem? I'm a newb, so maybe i missed something basic, like a header in the response


EDIT I've finally pinned the problem: I shouldn't have included the port number in my permission. Here's the wrong permission I wrote:

"permissions" : [
    "http://localhost:8080/" 
]

And here's the correct form:

"permissions" : [
    "http://localhost/" 
]

everything seems to works fine now.

like image 625
Eyal Avatar asked Apr 25 '11 23:04

Eyal


1 Answers

The problem was that I shouldn't have included the port number in my permission.

Here's the wrong permission I wrote:

"permissions" : [
    "http://localhost:8080/"
]

And here's the correct form:

"permissions" : [
    "http://localhost/"
]
like image 197
Eyal Avatar answered Nov 14 '22 16:11

Eyal