如何使用asp.net中的webservice从clintside输入数据?

时间:2013-12-17 09:37:20

标签: javascript asp.net ajax json web-services

我正在尝试这个:

这是我的javascript代码:

    function insertVisitor() {
        var pageUrl = '<%=ResolveUrl("~/QuizEntry.asmx")%>'
        $.ajax({
            type: "POST",
            url: pageUrl + "/insert_Visitor",
            data: "{'name':'" + $("#txtName").val() + "','phoneno':'" +
                  $("#txtPhone").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

        return false();
    }

    function OnSuccessCall(response) {
        window.open("about.html");
    }

    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }

</script>

这是html:

<form action="" method="post">
            <table>
                <tr>
                    <th>
                        <label>
                            please enter your Name :
                        </label>
                    </th>
                    <td>
                        <input id="txtName" type="text" value="" placeholder="Name" />
                    </td>
                </tr>
                <tr>
                    <th>
                        please enter your Phone No :
                    </th>
                    <td>
                        <input id="txtPhone" type="text" value="" placeholder="Phone No" />
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                    <!--<input id="btnEnter" type="button" style="background-image:~/images/pop_up/enter.png; height:40px; width:120px" />-->
                        <!--<input type="submit" name="button" id="button" value="enter" onClick="window.location='about.html'" />-->
                        <!--<a href="about.html" style="display: block">-->
                            <img src="images/pop_up/enter.png" width="120" height="40" alt="img" style="width: 120px;
                                height: 40px; position: relative; right: 0;" onclick="return insertVisitor()" />
                         <!--</a>-->
                    </td>
                </tr>
            </table>
            </form>

这是网络服务代码:

 [WebMethod]
public string insert_Visitor(string name, string phoneno)
{
    string returnvalue = "";
    if (name != "" && phoneno != "")
    {
        cmd.Parameters.Clear();
        cmd.Connection = connection;
        cmd.CommandText = "Insert_Into_VisitorLog";
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Phone", phoneno);

        Object obj = cmd.ExecuteScalar();

        if (obj != "0" || obj != "Unexpected error occurred!!")
        {
            returnvalue = Convert.ToString(obj);
        }
        else
        {
            returnvalue = Convert.ToString(obj);
        }
    }


    return returnvalue;
}

我面临的问题是收到这样的错误

400 Bad Request 

如何解决这个请帮助

1 个答案:

答案 0 :(得分:0)

jQuery中的数据部分是错误的

 data: "{'name':'" + $("#txtName").val() + "','phoneno':'" +
              $("#txtPhone").val() + "'}",

应该是

 data: {'name': $("#txtName").val()  ,'phoneno': $("#txtPhone").val() },

编辑1

同样,当您从服务中返回字符串时,{j}调用dataType也是错误的。

Differences between contentType and dataType in jQuery ajax function

编辑2

   var pageUrl = '<%=ResolveUrl("~/QuizEntry.asmx")%>'+"/insert_Visitor?name="+ $("#txtName").val()+"&phoneno="+ $("#txtPhone").val() ;
    $.ajax({
        type: "POST",
        url: pageUrl,
        success: OnSuccessCall,
        error: OnErrorCall
    });

编辑3

获取有关错误详细信息的更多信息,请使用以下

function OnErrorCall(xhr, ajaxOptions, thrownError)
{
   alert(xhr.responseText);
}