Phonegap Ajax调用返回内部服务器错误

时间:2014-10-20 13:58:29

标签: android jquery web-services cordova

我在Phonegap Android App Development中的新手。我正在尝试将我的应用程序与远程MS SQL Server数据库连接,我还编写了一个ASP.Net Web服务来连接这两个。 但是,当我通过JQuery ajax()方法调用此Web服务时,它返回"内部服务器错误"。

代码:

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

我的网络服务方法:

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

告诉我我做错了什么..... 在此先感谢.........

2 个答案:

答案 0 :(得分: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);
    }
});

答案 1 :(得分:0)

最后,我找到了解决方案Here

data: "{ firstName: 'Aidy', lastName: 'F' }"

然后,我修改了我的代码并且它已经工作了。

$.ajax({
      type: "POST",
      url: "http://my-domain.com/DocNote_WebService/DoctorMaster.asmx/insertDoctor",
      data: "{doctorName:'" + doctorName + "', contactNumber:'" + contactNumber + "', doctorType:'" + doctorType + "'}",
      contentType: "application/json; charset=utf-8",  
      dataType: "json",
      success : function(response) {            
          alert('Record Saved Sucessfully.....!!!!');                       
       },
       error: function(xhr, ajaxOptions, thrownError) {
           alert('ERROR: '+thrownError);
       }
});