0

This is my first post on stack overflow, so I'll try to respect conventions and be as clear as possible.

Introduction :

For this project I'm trying to doing a web app with a local web server. My project is divide in two parts :

  1. Local web server which transmit request to a real web server (it is a solution to resolve the Same origin policy)
  2. A android web app which is the view. It's where the problem occurred.

When I'm trying to communicate with the server, using ajax request, the error INVALID_STATE_ERR: DOM Exception 11 occurred. However when I'm doing it on a firefox (with Apache), I haven't this problem.

Local web server :

The web server only stock or transmit data between web app and web server.

Webapp :

Initialization

In my android activity I'm starting my web app like that :

 webview = new WebView(this);

 MyJavaScriptInterface myJavaScriptInterface = new MyJavaScriptInterface(this);
 webview.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");

 // set settings
 webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
 webview.getSettings().setJavaScriptEnabled(true);
 webview.getSettings().setAppCacheEnabled(false);
 webview.setHorizontalScrollBarEnabled(false);
 webview.setVerticalScrollBarEnabled(false);
 if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) // 16
 {
// yourwebview, i use phonegap here
   webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
    webview.getSettings().setAllowFileAccessFromFileURLs(true);
    }

    // start
    uri = new URI("http://127.0.0.1:"+LocalServer.RECORDING_PORT+"/index.html");
    webview.loadUrl(uri.toString());

Ajax

To save the data in my webapp I'm doing a basic XMLHttpRequest (also tried with JQuery but no message came out)

function saveObject(in_strAction, in_oData, fctCallback)
{

    var l_strURL = 'http://127.0.0.1:8888/api/' + in_strAction;
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        try // line 46
        {
            console.log ("xmlhttp.readyState="+xmlhttp.readyState+" && xmlhttp.status="+xmlhttp.status);
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                fctCallback(xmlhttp.responseText);
            }
        }
    }

    xmlhttp.open("POST",l_strURL,true);
    xmlhttp.send();
}

Responses

Answers received by the web app are json and headers are create by the web server.

4

1 回答 1

0

Problem solved

The exception INVALID_STATE_ERR: DOM Exception 11 is up while reading an invalid status :

function saveObject(in_strAction, in_oData, fctCallback)
{

    var l_strURL = 'http://127.0.0.1:8888/api/' + in_strAction;
    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {
        try
        {
            // Don't read status here (on state 1 exception will be up)
            console.log ("xmlhttp.readyState="+xmlhttp.readyState);
            if (xmlhttp.readyState==4)
            {
                // Status can be read here.
                console.log ("xmlhttp.status="+xmlhttp.status);
                if (xmlhttp.status==200)
                {
                    fctCallback(xmlhttp.responseText);
                }
            }
        }
    }

    xmlhttp.open("POST",l_strURL,true);
    xmlhttp.send();
}
于 2013-06-10T10:59:50.790 回答