使用JQUERY ajax函数将JSON对象发送到ASP.NET Web服务 [英] Sending a JSON object to an ASP.NET web service using JQUERY ajax function

查看:88
本文介绍了使用JQUERY ajax函数将JSON对象发送到ASP.NET Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在aspx页面的客户端上创建对象.我想将功能添加到这些javascript类中,以使生活更轻松.

I want to create object on the client side of aspx page. And i want to add functions to these javascript classes to make easier the life.

实际上,我可以获取和使用从服务返回的对象(从服务器端类派生).当我想通过jquery ajax方法从客户端发送对象时,我做不到:)

Actually i can get and use the objects (derived from the server side classes) which returns from the services. When i wanted to send objects from the client by jquery ajax methods, i couldn't do it :)

这是我的javascript类:

This is my javascript classes:

function ClassAndMark(_mark, _lesson){

    this.Lesson = _lesson;
    this.Mark = _mark;
}


function Student(_name, _surname, _classAndMark){

    this.Name = _name;
    this.SurName = _surname;
    this.ClassAndMark = _classAndMark;
}

这是Student类调用Web服务的方法:

And this is a method for Student class to call Web Service:

JSClass.prototype.fSaveToDB(){
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/WS/SaveObject.asmx/fSaveToDB"),

        data: ????????????,
 // This might be: JSON.stringify(this) ?
 // Web service method has a parameter, name is _obj 
 // if i don't send data with parameter, i'm getting this error:
 // Invalid web service call, missing value for parameter: '_obj'
 //
 // Should i send it like that:
 // data: "{_obj:" + JSON.stringify(this) + "}"
 // 
 // I tried to wrap this with parameter like that: 
 // data: JSON.stringify("{_obj:" + this + "}") 
 // 
 // But i got this error:
 // Cannot convert object of type 'System.String' to type 'System.Collections.Generic.IDictionary`2[System.String,System.Object]'

        dataType: "json"
    });
}

要创建javascript对象并调用其方法以将其发送到网络服务:

To create javascript object and call its method to send it toward web service:

实际上,我不知道服务器端的类和方法的定义是什么,但我认为:

Actually i don't know what should be definition of classes and methods on the Server side but i think:

class ClassAndMark{

    public string Lesson ;
    public string Mark ;
}


class Student{

    public string Name ;
    public string SurName ;
    public ClassAndMark classAndMark ;
}

Web服务在下面,但是我再也无法获得应该的东西,而不是那个???? :

Web service is below but again i couldn't get what should be instead of the ???? :

[WebMethod()]
public Student fSaveToDB(???? _obj)
{
    // How can i convert input parameter/parameters 
    // of method in the server side object?   

    // SQL operations  
    // srting QInsert = "INSERT INTO tableName (......) VALUES (.....)";
    // ...
    // ..
    // .

    return new Student{
                     Name = ???, // deserialize _obj and pass its Name value
                     SurName = ???, // deserialize _obj and pass its SurName value
                     classAndMark = ???, // deserialize _obj and pass its classAndMark value
                  };
}

推荐答案

步骤1,客户端: 您必须将客户端对象序列化为JSON,我个人使用JSON2库的stringify()方法: http://www.json.org/js.html

Step 1, client side: You have to serialize your client objects into JSON, I personally use stringify() method of the JSON2 library: http://www.json.org/js.html

data: JSON.stringify(myObj)

第2步,服务器端:您必须使用c#代码将序列化的对象转换为可食用"的对象.在这里,您可以使用Microsoft JavaScriptSerializer类的deserialize()方法(但如果未安装SP,则在.net 3.5中可能会有一些问题),否则可以使用JSON.net库

Step2, Server-side: You have to translate the serialized object into something "eatable" by your c# code. Here you can use deserialize() method of Microsoft's JavaScriptSerializer class (but it might have some issues in .net 3.5 if you don't have SP installed), or otherwise JSON.net library http://james.newtonking.com/pages/json-net.aspx

服务器端方法签名应为:

server-side method signature should be:

fSaveToDB(Object myObj)

其中"myObj"是客户端对象容器的名称:

where "myObj" is the name of your client-side object container:

{myObj: your object...}

这篇关于使用JQUERY ajax函数将JSON对象发送到ASP.NET Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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