0

jquery.mobile-1.1.1 jquery-1.7.2.min cordorva-1.9.0

我才刚刚开始使用它并且已经搞砸了一个星期......我已经阅读了那里的每一篇文章并尝试了我能想到的所有组合,但仍然没有任何效果。作为最后的手段,我将它从 phonegap 移到了一个真正的浏览器页面,我得到了一些未定义的错误:

// if these are NOT set I get the "NO Transport" error
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;

// however, with the above defined (true), the following is the error:
// xhr.statusText = "error"
// textStatus = "error"
// errorThrown.message = "Access Denied"


function login(username, password)
{
  var serviceURL = "http://mobile.mydomain.com/";
  var strData = "username="+username +"&password="+password;

  // setup the paramaters for the ajax call
  var ajaxParams = {  
      type: 'POST',
      url: serviceURL + 'mypage.php',
      data: strData,
      datatype: 'json',
      success: function(xmlData, textStatus, xhr) { onLoginOK(xmlData, textStatus, xhr) },
      error: function(xhr, textStatus, errorThrown) { 
                      onLoginFailure(xhr, textStatus, errorThrown) },
      timeout: 5000,
      cache: false
  };

  // make the ajax call
  $.ajax(ajaxParams);
}

function onLoginOK(data, textStatus)
{
  // probably not right but haven't gotten this far!
  var what = jQuery.parseJSON(data); 

  alert("Login results: " + data); 
  if(what.status == "OK")
  {
    localStorage.setItem("door", what.username);
    localStorage.setItem("knock", what.password);
    localStorage.setItem("house", what.userid);
  }
  else if(what.status == "FAILED")
  {
      alert("Login failed: " +what.text);
  }
}

function onLoginFailure(xhr, textStatus, errorThrown)
{
  alert("There was an error: The server reported: " + xhr.responseText 
        +"\nstatus: " +textStatus 
        + "\nerror: " +errorThrown);
}

在 cordova.xml 中,我允许通过以下方式访问我的域:

<access origin="http://mobile.mydomain.com" subdomains="true "/> 

有人有什么建议吗?

==================================================== ======================这是来自eclipse:

07-27 09:12:16.612: I/dalvikvm(1295): Could not find method android.webkit.WebView.<init>, referenced from method org.apache.cordova.CordovaWebView.<init>
07-27 09:12:16.612: W/dalvikvm(1295): VFY: unable to resolve direct method 3116: Landroid/webkit/WebView;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;IZ)V
07-27 09:12:17.339: I/webviewdatabase(1295): openOrCreateDatabase1
07-27 09:12:17.362: I/CordovaLog(1295): Changing log level to DEBUG(3)
07-27 09:12:17.362: I/CordovaLog(1295): Found preference for useBrowserHistory=false
07-27 09:12:17.378: E/dalvikvm(1295): Could not find class 'android.webkit.WebResourceResponse', referenced from method org.apache.cordova.CordovaWebViewClient.generateWebResourceResponse
07-27 09:12:17.378: W/dalvikvm(1295): VFY: unable to resolve new-instance 451 (Landroid/webkit/WebResourceResponse;) in Lorg/apache/cordova/CordovaWebViewClient;
07-27 09:12:17.378: W/dalvikvm(1295): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
07-27 09:12:17.386: W/dalvikvm(1295): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
07-27 09:12:17.386: W/dalvikvm(1295): VFY: unable to find class referenced in signature (Landroid/webkit/WebResourceResponse;)
07-27 09:12:17.386: I/dalvikvm(1295): Could not find method android.webkit.WebViewClient.shouldInterceptRequest, referenced from method org.apache.cordova.CordovaWebViewClient.shouldInterceptRequest
07-27 09:12:17.386: W/dalvikvm(1295): VFY: unable to resolve virtual method 3127: Landroid/webkit/WebViewClient;.shouldInterceptRequest (Landroid/webkit/WebView;Ljava/lang/String;)Landroid/webkit/WebResourceResponse;
07-27 09:12:19.706: W/dalvikvm(1295): No implementation found for native Lcom/carrieriq/iqagent/client/NativeClient;.clientInit (Ljava/lang/Object;)I
07-27 09:12:21.979: I/Database(1295): sqlite returned: error code = 14, msg = cannot open file at source line 25467
07-27 09:12:22.198: I/Web Console(1295): Error: SyntaxError: Unexpected token ILLEGAL at file:///android_asset/www/cordova-1.9.0.js:1012
07-27 09:12:22.347: I/Flex(1295): loadOperatorCode() strOp is AT&T
07-27 09:12:22.347: I/Flex(1295): loadOperatorCode() strMccList is 
07-27 09:12:22.347: I/Flex(1295): loadOperatorCode() cr.query strMCC strMNC 
07-27 09:12:22.354: I/Flex(1295): loadOperatorCode() mOperatorIndex ==> 1
07-27 09:12:24.167: I/Web Console(1295): **Error** initializing Network Connection: Class not found at file:///android_asset/www/cordova-1.9.0.js:5317
07-27 09:12:26.854: I/Web Console(1295): **JSCallback Error:** Request failed. at file:///android_asset/www/cordova-1.9.0.js:3747
07-27 09:12:28.331: I/Database(1295): sqlite returned: error code = 14, msg = cannot open file at source line 25467
07-27 09:12:28.347: I/Web Console(1295): **Error:** SyntaxError: Unexpected token ILLEGAL at file:///android_asset/www/cordova-1.9.0.js:1012
07-27 09:12:28.386: I/Web Console(1295): **Error** initializing Network Connection: Class not found at file:///android_asset/www/cordova-1.9.0.js:5317
4

2 回答 2

4

首先,您应该在添加您的 cordova 或 JQM 脚本之前添加这些

<script type="text/javascript">
$(document).bind("mobileinit", function() {
    $.support.cors = true;
    $.mobile.allowCrossDomainPages = true;
});
</script>

然后你应该像这样发出一个ajax请求

$
.ajax({
    cache : false,
    type : 'POST',//While GET working
    async: false,
    url : "http://192.168.1.100/something.xml" + "?time=" + Date.now(),
    data : {
        key : "value"
        },
    dataType : "xml",
    success : function(xml) {
            },
    error : function(xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
    }
});

如果这不适合您使用XMLHttpRequest

var req = new XMLHttpRequest();
req.open("POST", "http://192.168.1.100/something.JPG", true);
req.setRequestHeader("Content-type", "image/jpg");

req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if (req.status == 200) {
            var data = req.responseText;
        }
    }
};
req.send(null);
于 2012-07-27T06:07:01.497 回答
0

你得到什么错误?你能发布来自 FireBug 或 IE 开发者工具的请求和响应文本吗?

查看您的代码,如果您还没有的话,我有一些想法可以尝试:

1) 指定内容类型。

 contentType: "application/json; charset=utf-8" // <-- or whatever contentType

2) 如果 mypage.php 需要请求正文中的用户名和密码,则尝试将 strData 更改为 json

strData = {
     username:username, password:password
}

3)和/或字符串化它取决于 mypage.php 是否需要一个对象或一个字符串

JSON.stringify(strData);

此外,由于您的处理程序具有与匿名处理程序函数完全相同的参数,因此您可以替换:

success: function(xmlData, textStatus, xhr) {
         onLoginOK(xmlData, textStatus, xhr) 
},
error: function(xhr, textStatus, errorThrown) { 
       onLoginFailure(xhr, textStatus, errorThrown) 
},

直接分配:

success: onLoginOK,
error: onLoginFailure,

通过消除可能的错误来源,可以使调试更容易一些。

生成的代码可能如下所示...

function login(username, password)
{
    var serviceURL = "http://mobile.mydomain.com/";
    var strData = JSON.stringify({
        username:username, password:password
    });

  // setup the paramaters for the ajax call
  var ajaxParams = {  
      type: 'POST',
      url: serviceURL + 'mypage.php',
      data: strData,
      datatype: 'json',
      contentType: "application/json; charset=utf-8" // <-- or whatever contentType 
      success: onLoginOK,
      error: onLoginFailure,
      timeout: 5000,
      cache: false
  };

  // make the ajax call
  $.ajax(ajaxParams);
}

查看请求和响应文本真的很有帮助。如果以上都没有帮助,您可以将它们附加到您的回复中吗?

于 2012-07-26T22:21:35.663 回答