BlackBerry - Browserfield XMLHttpRequest not valid

BlackBerry - Browserfield XMLHttpRequest not valid

本文关键字:not valid XMLHttpRequest Browserfield BlackBerry      更新时间:2023-09-26

我正在尝试从javascript中启动一个浏览器字段内的ajax请求。

这是一个演示应用程序,它只是一个html元素,试图在点击时发出ajax请求。

public MyScreen() {
    // Set the displayed title of the screen
    setTitle("MyTitle");
    BrowserFieldConfig _myBrowserFieldConfig = new BrowserFieldConfig();
    _myBrowserFieldConfig.setProperty(
            BrowserFieldConfig.JAVASCRIPT_ENABLED, Boolean.TRUE);
    _myBrowserFieldConfig.setProperty(BrowserFieldConfig.ALLOW_CS_XHR,
            Boolean.TRUE);
    BrowserField browser = new BrowserField(_myBrowserFieldConfig);
    browser.displayContent(
            "<!DOCTYPE html><html><head>"
                    + "<script>function loadXMLDoc(){"
                        + "alert('t'); "
                        + "var xmlhttp;"
                        + "if (window.XMLHttpRequest) "
                        +"{ "
                            + "xmlhttp = new XMLHttpRequest();  "
                        + "} "
                        + "else { "
                            + "xmlhttp = new ActiveXObject('"Microsoft.XMLHTTP'");  "
                        + "}"
                        + " xmlhttp.onreadystatechange=function()  "
                        +"{ "
                            + " if (xmlhttp.readyState==4 && xmlhttp.status==200) "
                            + "{"
//                                  + " document.getElementById('"myDiv'").innerHTML=xmlhttp.responseText;"
                            + "}"
                            +"alert('State:'+xmlhttp.readyState+ 'status'+ xmlhttp.status)"
                        + "} "
                        + "xmlhttp.open('"GET'",'"http://www.w3schools.com/ajax/demo_get.asp'",true);"
                        + "xmlhttp.send(); "
                        + "}"
                    + "</script>"
                    + "</head><body><h2>AJAX</h2><a onclick='"javascript:loadXMLDoc();'">Request data</a><div id='"myDiv'"></div></body></html>",
            "http://www.w3schools.com");
    add(browser);
}

问题是,即使第一个警报是不工作的,所以应该有一个javascript语法错误,但好吧,一切看起来很好我。我在我的电脑上用Firefox试了试这段代码,警报显示出来了(我知道跨域并不总是可能的)。那么,我的脚本中的错误是什么呢?

我已经在BB 5.0.0和7.0.0的模拟器上测试了这个

我还没有检查出你的javascript,但我之前遇到过问题,当我尝试在之前请求BrowserField 中的内容时,该字段实际上添加到其包含ManagerScreen

输入这一行:

add(browser);

之前调用

browser.displayContent(/* content here */);

Update:当我在9550 5.0模拟器中运行您的代码时,我看到了同样的问题。然而,当一长串html和脚本被分成许多行,并带有引号、加号和转义字符时,很难调试它。一般来说,如果HTML是静态的,我建议把它放在一个. HTML文件中,作为资源与应用捆绑在一起。

所以,我试着把你的html字符串的全部内容放入我的项目中的一个文件,在/res文件夹下,命名为content.html:

<!DOCTYPE html>
<html>
    <head>
         <script>
                function loadXMLDoc(){
                    alert('t');
                    var xmlhttp;
                    if (window.XMLHttpRequest)
                    {
                        xmlhttp = new XMLHttpRequest();
                    }
                    else {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.onreadystatechange=function()
                    {
                        if (xmlhttp.readyState==4 && xmlhttp.status==200)
                        {
                               //document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                        }
                        alert('State:'+xmlhttp.readyState+ 'status'+ xmlhttp.status)
                    }
                    xmlhttp.open("GET","http://www.w3schools.com/ajax/demo_get.asp",true);
                    xmlhttp.send();
                }
           </script>
      </head>
      <body><h2>AJAX</h2><a onclick="javascript:loadXMLDoc();">Request data</a><div id="myDiv"></div>
      </body>
</html>

,然后我成功地看到它加载并执行javascript onclick使用以下Java:

   public MyScreen() {
      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
      browser = new BrowserField();
      add(browser);
      InputStream content = getClass().getResourceAsStream("/content.html");     
      try {
         byte[] html = IOUtilities.streamToBytes(content);
         browser.displayContent(new String(html), "http://localhost");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }

所以,我建议你这样做。(注意:我不认为我使用的localhost基础URL很重要……只有当你的HTML包含相对链接和路径时,这才重要)

错误的是使用绝对url打开XMLHttpRequest。它需要使用一个相对url(例如:demo_get.asp),并且browserfield需要正确设置baseurl (http://www.w3schools.com/ajax/)。