阿贾克斯jQuery的传递多个参数的Web服务 [英] Ajax jquery passing multiple parameters web services

查看:120
本文介绍了阿贾克斯jQuery的传递多个参数的Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
        $('#btnregister').click(function () {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "fetchusers.asmx/RegUsers",
                data: "{ username: '" + $("#txtuser").val() + "name: '" + $("#txtname").val() + "'}",
                dataType: "json",
                success: function (data) {
                    alert("Successfully register");
                    $("#btnregclose").click();
                }
            });
        });
    </script>

<div id="registration">
        <fieldset>
            <legend>Registration Form</legend>
                <input id="txtuser" type="text" placeholder="Username" /><br />
                <input id="txtname" type="text" placeholder="Name" /><br />
                <input id="txtpass" type="password" placeholder="password" /><br />
                <input id="txtconfirmpass" type="password" placeholder="confirm password" /><br />
                <input id="btnregister" type="button" value="Register" />
                <input id="btnregclose" type="button" value="close" />
        </fieldset>
    </div>


[WebMethod]
        public string RegUsers(string username, string name)
        {
            string response = username + name;

            return response;
        }

我在阿贾克斯jQuery的初学者,我做运动来改善一下我的知识。我的问题是,当我点击#btnregister它不工作。我觉得有一个在我的AJAX传递的参数有问题,但我不知道它是什么。

I'm a beginner in Ajax Jquery and I'm doing exercise to improve my knowledge about it. My problem is when I click #btnregister it's not working. I think there's a problem in the parameters I passed on the ajax but I don't know what is it.

推荐答案

试试这个:

 $(document).ready(function () {
        $('#btnregister').click(function () {
            var obj = { username: $("#txtuser").val(), name: $("#txtname").val() };
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "fetchusers.asmx/RegUsers",
                data: JSON.stringify(obj),
                dataType: "json",
                success: function (data) {
                    alert("Successfully register");
                    $("#btnregclose").click();
                }
            });
        });
    });

这曾在我的本地环境。

这篇关于阿贾克斯jQuery的传递多个参数的Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆