1

我有以下 jQuery ajax 代码,它调用 ASP.NET webservice 将数据保存在数据库中。

function SaveDataToParty() {
            var mode = "Save";
            var srno = 0;
            var partyname = $("#p1").val();
            var partyemail = $("#p2").val();
            var partyphone = $("#p3").val();
            var partyaddress = $("#p4").val();
            var webMethod = "../Service/DatabaseHandling.asmx/SaveDataToParty";
            var parameters = "{mode:'" + mode + "',srno:'" + srno + "',partyname:'" + partyname + "',partyemail:'" + partyemail + "',partyphone:'" + partyphone + "',partyaddress:'" + partyaddress + "'}";
            alert(parameters);
            $.ajax({
                type: "POST",
                url: webMethod,
                async:false,
                data: JSON.stringify(parameters),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    alert(msg.d);
                },
                error: function (e) {
                    alert(e.statusText);
                }
            });
        }

此调用遵循 asp.net 中的 webservice 方法

<WebMethod()> _
Public Function SaveDataToParty(mode As String, srno As String, partyname As String, partyemail As String, partyphone As String, partyaddress As String) As Boolean
    'this will start connection and set transaction to it
    Dim cnstr As String = String.Empty
    cnstr = System.Configuration.ConfigurationManager.ConnectionStrings("DPMTConnectionString").ConnectionString
    Dim cn As New SqlConnection(cnstr)
    Dim tran As SqlTransaction
    cn.Open()
    tran = cn.BeginTransaction
    Try
        'this will handle sp coding
        Dim dcmd As New SqlCommand
        dcmd.Connection = cn
        dcmd.CommandType = CommandType.StoredProcedure
        'sp execution for party entry
        If mode = "Save" Then
            dcmd.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "ADD"
            dcmd.Parameters.Add("@srno", SqlDbType.Int).Value = 0
        End If
        If mode = "Update" Then
            dcmd.Parameters.Add("@mode", SqlDbType.NVarChar).Value = "UPDATE"
            dcmd.Parameters.Add("@srno", SqlDbType.Int).Value = srno
        End If

        dcmd.Parameters.Add("@partyname", SqlDbType.NVarChar).Value = partyname
        dcmd.Parameters.Add("@partyemail", SqlDbType.NVarChar).Value = partyemail
        dcmd.Parameters.Add("@partyphone", SqlDbType.NVarChar).Value = partyphone
        dcmd.Parameters.Add("@partyaddress", SqlDbType.NVarChar, 500).Value = partyaddress

        dcmd.CommandText = "sp_add_update_delete_party"
        dcmd.Transaction = tran
        dcmd.ExecuteNonQuery()
        tran.Commit()
        Return True
    Catch ex As Exception
        Return False
    Finally
        cn.Close()
    End Try
End Function

该错误仅在 Internet Explorer 10 中生成。错误是“无效的 Web 服务调用,参数缺失值:\u0027mode\u0027”

它在谷歌浏览器中运行良好。

有什么解决办法吗?

编辑 1

我已经用 JSON.stringify 和 async:false 更新了这个,但在 IE10 上仍然没有成功。

编辑 2

在 IE10 开发者工具中,在网络选项卡中,显示以下信息。

Key Value
Request POST /hemal/ASPNET/DPMT/Service/DatabaseHandling.asmx/SaveDataToParty HTTP/1.1
Accept  application/json, text/javascript, */*; q=0.01
Content-Type    application/json; charset=utf-8
X-Requested-With    XMLHttpRequest
Referer http://localhost/hemal/ASPNET/DPMT/ADMIN/Purchase_Add.aspx
Accept-Language en-IN,en-GB;q=0.8,en;q=0.5,gu;q=0.3
Accept-Encoding gzip, deflate
User-Agent  Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Host    localhost
Content-Length  0
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache
Cookie  ASP.NET_SessionId=mhp35fbfn3vzoalqvdild5hb; loginstatus=ok

我很震惊为什么 content-length 为 0。这意味着什么都没有发送。

4

1 回答 1

0

我无法复制它;但是,对我来说似乎可疑的一件事是您正在对自己的 json 进行字符串化,即

var parameters = "{mode:'" + mode + "',srno:'" + srno + "',partyname:'" + partyname + "',partyemail:'" + partyemail + "',partyphone:'" + partyphone + "',partyaddress:'" + partyaddress + "'}";

您的实现与 JSON2.js 实现之间存在差异。你可以写:

JSON.stringify({
            mode: mode,
            srno: srno,
            partyname: partyname,
            partyemail: partyemail,
            partyaddress: partyaddress
        }));

看:

http://jsfiddle.net/DwTge/9/

注意参数名称引用的区别?

于 2013-08-23T13:14:24.233 回答