0
我正在嘗試使Anroid移動應用程序將數據保存到遠程SQL數據庫。我正在使用Visual Studio 2015中的科爾多瓦項目。我有一個Web服務工作正常,並取名稱,電話&配置文件信息並正確保存在SQL Server中的數據。現在我正在嘗試從Android Mobile Application調用此WebService,但$ Ajax Call to webservice無法正常工作。控件沒有到達Web服務。用Ajax保存數據以Json作爲參數調用WebService不起作用在Android的Cordova項目中
我已經添加了WhiteSpace插件,但它仍然無法正常工作。
這裏是原始的HTML文件
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script src="../scripts/jquery-3.1.0.min.js"></script>
<script src="../scripts/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" charset="utf-8">
function onLoad() { document.addEventListener("deviceready", onDeviceReady, false); }
function onDeviceReady() { alert("Device is Ready to User"); }
function SaveContact() {
var Contact = new Object();
Contact.Name = "Harvinder";
Contact.Phone = "9819862717";
Contact.Profile = "Director"
var myData = JSON.stringify(Contact);
var myURL = "http://gsecurity.net/JsonDAL.asmx/SaveContact";
alert(typeof (myData));
$.ajax({
type: "POST", url: myURL, data: myData, contentType: "application/json; charset=utf-8", dataType: "json",
success: function (msg) { alert("Success"); },
error: function (e, status) { alert("Failed:" + e.statusText); }
});}
</script>
</head>
<body onload="onLoad()">
<h1>Call Ajax to Save Json Data with WebService</h1>
<button onclick="SaveContact();">Click Here to Save Test Contact</button>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/appBundle.js"></script>
</body>
</html>
下面是修改後的代碼按建議其仍然沒有工作
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline' 'unsafe-eval'; script-src * 'unsafe-inline' 'unsafe-eval'">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() { alert("Version 1.10 is Ready"); }
$.support.ajax = true;
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
function SaveContact() {
var Contact = new Object();
Contact.Name = "Harvinder";
Contact.Phone = "9819862717";
Contact.Profile = "Director";
var myData = JSON.stringify(Contact);
var myURL = "http://gsecurity.net/JsonDAL.asmx/SaveContact";
alert("DataType:" + typeof (myData));
$.ajax({
type: "POST", contentType: "application/json", dataType: "json",
url: myURL, data: myData, allowCrossDomainPages: true,
success: function (msg) { alert("Success"); },
error: function (e, status) { alert("Failed:" + e.statusText); }
});
alert("Completed");
}
</script>
</head>
<body>
<h2>Call Ajax to Save Json Data with WebService</h2>
<button onclick="SaveContact()">Click Here to Save Test Contact</button>
<script type="text/javascript" src="../scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="../scripts/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="scripts/platformOverrides.js"></script>
<script type="text/javascript" src="scripts/appBundle.js"></script>
</body>
</html>
我的Webservice,在這裏,當通過網址直接訪問其工作正常
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
<System.Web.Script.Services.ScriptService()>
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)>
<ToolboxItem(False)>
Public Class JsonDAL
Inherits System.Web.Services.WebService
<WebMethod>
Public Function SaveContact(Name As String, Phone As String, Profile As String) As String
Try
Dim ConStr = ConfigurationManager.ConnectionStrings("Data").ConnectionString
Dim Con = New SqlConnection(ConStr)
Dim Qry = "INSERT INTO Contacts (Name,Phone,Profile) VALUES ('" + Name + "','" + Phone + "','" + Profile + "')"
Dim Cmd = New SqlCommand(Qry, Con)
Cmd.CommandType = CommandType.Text
Con.Open()
Cmd.ExecuteNonQuery()
Con.Close()
Return "Done"
Catch ex As Exception
Return ex.Message
End Try
End Function
End Class
感謝您Response.I複製你的代碼,因爲它是但它仍然沒有工作。我在我的Android手機中安裝了.apk,但它沒有超出線路警報(typeof(myData))。我看到在數據庫中添加了一條記錄TestFromHomen。當我運行代碼在給出錯誤 { {message}:「Uncaught ReferenceError:$ is not defined」, 「filename」:「http://stacksnippets.net/js」, 「lineno」:33, 「colno」:17 } – Saimbhi
jquery和jqury移動js文件在腳本文件夾中嗎? – Homen
是的.js文件都在那裏。相同的代碼在正常的asp.net項目中工作正常,但在Cordova項目中不起作用。這意味着代碼和web服務是完美的。我想某些地方是停止Android應用程序訪問外部URL的地方是錯誤的。我也改變了meta標籤,但它仍然不起作用。 – Saimbhi