开发者

JSON generation problem

开发者 https://www.devze.com 2023-04-01 00:53 出处:网络
This way i am trying to generate JSON but i am getting error. var DTO = { \"UserID\": $(\'txtUserID\').val(),

This way i am trying to generate JSON but i am getting error.

var DTO =
        {
            "UserID": $('txtUserID').val(),
            "Pwd": $('txtPassword').val(),
            "isPersistent": $('#chkRemember').attr('checked')
        };

here this way i am sending json data to my server side function'

 $(document).ready(function () {
    $("#btnSubmit").click(function () {
        var DTO =
        {
            "UserID": $('txtUserID').val(),
            "Pwd": $('txtPassword').val(),
            "isPersistent": $('#chkRemember开发者_如何学JAVA').attr('checked')
        };

        alert(DTO);
        alert(JSON.stringify(DTO));
        $.ajax({
            type: "POST",
            url: "CateGory.aspx/GetData",
            data: JSON.stringify(DTO),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                sHtml = data.d;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }

        });

        return false;
    })
});

am i making any mistake because i am sending multiple parameter to server side function.

my server side function look like

[WebMethod]
    public static string Authenticate(string UserID,string Pwd,bool isPersistent)
    {
        return "SUCCESS";
    }

so please guide me that how to wrap up my multiple argument with data in DTO object and later we can issue data: JSON.stringify(DTO).


First: you don't need to stringify this. Second: Do a GET instead of a POST, because you have multiple arguments.

You can also create a new class:

class SomeObjectToBePosted {
    string UserID { get;set; }
    string Pwd { get;set; }
    bool isPersistant { get;set; }
}

And let that be the parameter for your WebMethod. Then you can use a POST again.


What error do you get?

Using jQuery you don't need to stringify the data object, just write: data: DTO


Looks like you url is wrong. I think you are calling a web service. And also you dont have to stringify the data because it is already well form JSON

Change

url: "CateGory.aspx/GetData",
data: JSON.stringify(DTO)

To

url: "CateGory.asmx/GetData",
data: DTO
0

精彩评论

暂无评论...
验证码 换一张
取 消