我是Phonegap Android應用程序開發的新手。我試圖將我的應用程序與遠程MS SQL Server數據庫連接起來,我寫了一個ASP.Net Web服務來連接這兩個。 但是當我通過JQuery ajax()方法調用這個Web服務時,它返回「Internal Server Error」。Phonegap Ajax調用返回內部服務器錯誤
代碼:
<html>
<head>
<title>PhoneGap Example</title>
<script type="text/javascript" charset="utf-8" src="js/cordova.js"></script>
<link rel="stylesheet" href="css/jquery.mobile.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.mobile.js"></script>
<script type="text/javascript" >
function button_clicked(){
var name = $.trim($("#txtName").val());
var contact = $.trim($("#txtContactNumber").val());
var type = $.trim($("#txtType").val());
if(name.length > 0)
{
$.ajax({
type: "POST",
url: "http://my-domain.com/DocNote_WebService/DoctorMaster.asmx/insertDoctor",
data: "{doctorName: "+ name + ",contactNumber: "+ contact + ",doctorType: " + type +"}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success : function(data) {
alert('Record Saved Sucessfully.....!!!!');
},
error: function(xhr, ajaxOptions, thrownError) {
alert('ERROR: '+thrownError);
}
});
}
}
</script>
</head>
<body>
<section id="page1">
<header><h1>DocNote</h1></header>
<div class="content" data-role="content">
<h3>Enter Doctor Info</h3>
<div data-role="fieldcontain">
<input type="text" data-clear-btn="true" name="txtName" id="txtName" placeholder="Enter Name"/>
<br/>
<input type="text" data-clear-btn="true" name="txtContactNumber" id="txtContactNumber" placeholder="Contact Number"/>
<br/>
<input type="text" data-clear-btn="true" name="txtType" id="txtType" placeholder="Type"/>
<br/>
<button id="btnSubmit" class="ui-btn ui-btn-inline ui-corner-all" onclick="button_clicked()">Submit</button>
<button id="btnCancel" class="ui-btn ui-btn-inline ui-corner-all">Cancel</button>
</div>
</div>
</section>
</body>
</html>
我的Web服務方法:
[WebMethod]
public void insertDoctor(String doctorName, String contactNumber, int doctorType) {
using (SqlConnection connection = ConnectionFactory.getConnection())
{
SqlCommand sqlCommand = new SqlCommand("Insert into DOCTOR_MASTER (DOCTOR_NAME, DOCTOR_CONTACT_NUMBER,DOCTOR_TYPE) values (@name,@contact,@type)",connection);
sqlCommand.Parameters.AddWithValue("@name", doctorName);
sqlCommand.Parameters.AddWithValue("@contact",contactNumber);
sqlCommand.Parameters.AddWithValue("@type", doctorType);
sqlCommand.ExecuteNonQuery();
}
}
告訴我我提前做錯了..... 感謝...... ...
您是否嘗試過調試服務器上的代碼以查看導致錯誤的原因?沒有這個,「內部服務器錯誤」根本沒有任何幫助。 – Archer 2014-10-20 14:30:31
嘗試使用request.form [「keyword」]在服務器端獲取發佈的數據並以這種方式發佈數據: data:{name:name,contact:contact,type:type} – Emre 2014-10-21 07:45:51
@Archer:我測試了我的Web服務使用[測試工具](https://wizdl.codeplex.com/),並且工作正常。 – 2014-10-21 21:11:10