2012-07-26 110 views
0

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

我剛開始接觸這個並一直在使用它一個星期了...我已經閱讀了每篇文章,嘗試了所有我能想到的組合,但仍然沒有任何工作。作爲最後的手段,我搬到了這一點的PhoneGap和一個真正的瀏覽器頁面,我得到一些不確定的錯誤:

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

// 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內

任何人有任何建議?

============================================== ========================== 這是日食:

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 

回答

0

你什麼錯誤?您可以發佈FireBug或IE Developer Tools的請求和響應文本嗎?

看你的代碼,我有一些想法,如果你還沒有準備好,你可以嘗試:

1)指定的contentType。

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); 

此外,由於你的處理程序有相同的PARAMS作爲匿名處理函數可以更換:

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); 
} 

這將真正幫助查看請求和響應文本。如果以上都不能幫助您將它們附加到您的回覆中?

+0

我在IE9中收到「訪問被拒絕」錯誤。在Firefox中xhr只顯示錯誤。我確實簡化了onSuccess/OnFailure,但沒有改變。不知道爲什麼我們需要將數據串聯起來,因爲這應該是一個POST,在這種情況下,後端將期望提取腳本post/get處理程序中的變量。此外,內容類型應該保留爲表單,如果不是的話? – ppetree 2012-07-27 11:57:42

4

首先,你應該將你的科爾多瓦或JQM腳本之前添加這些

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

那麼你應該作出這樣

$ 
.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); 
    } 
}); 

一個Ajax請求。如果這樣做對你來說不是作品利用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); 
+0

無論我在哪裏設置$ .support.cors = true;或$ .mobile.allowCrossDomainPages = true; - 我已經嘗試設置它們,並且在調用之前,但這不是問題。 – ppetree 2012-07-27 11:53:43

+0

這是在mobileinit中定義的標準,這裏解釋http://jquerymobile.com/test/docs/pages/phonegap.html – 2012-07-27 11:56:50

+0

你試過XmlhttpRequest? – 2012-07-27 12:08:01