JSON Ajax和参数传递 [英] Json ajax with parameter passing

查看:136
本文介绍了JSON Ajax和参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function BindJson() {
        $.ajax({
            type: "POST",
            url: "NewPage.aspx/SerializeJson",
            data: "{}",
            contentType: "application/json",
            dataType: "json",
            success: function (data1) {
                alert(data1);
            }
        })
    }
    [WebMethod]
        public static string SerializeJson()
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            //Person p2 = js.Deserialize<Person>(str);
            return "";
        }

我如何通过参数的数据我serializeJson功能?

How do I pass parameters as data to my serializeJson function?

推荐答案

这会为你(充分的工作code下面的示例)工作。关键是在一个Person对象通过。另外,我用一个简单的Web服务(myService.asmx),而不是一个aspx页面。为什么要额外的开销打扰如果不需要它?

This will work for you (full working code sample below). The key is to pass in a Person object. Also, I used a simple web service (myService.asmx) instead of an aspx page. Why bother with the extra overhead if it isn't needed?

最关键的是,在客户端上,创建一个Person对象,然后使用的 JSON.stringify 以Person对象传递给web服务。

The key is, on the client, create a Person object and then use JSON.stringify to pass the Person object to the webservice.

的JavaScript

<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.5.1.js"></script>
<script type="text/javascript">
    function BindJson() {
        $.ajax({
            type: "POST",
            url: "myService.asmx/SerializeJson",
            data: JSON.stringify({ person:{ firstName: "Denny", lastName: "Cherian", department: "Microsoft PSS", address: { addressline1: "Microsoft India GTSC", addressline2: "PSS - DSI", city: "Bangalore", state: "Karnataka", country: "India", pin: "560028" }, technologies: ["IIS", "ASP.NET", "JavaScript", "AJAX"] }}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data1) {
                alert(data1.d);
            },
            error: function (request, status, errorThrown) {
                alert(status);
            }
        });
    }

    $(document).ready(function() {
        BindJson();    
    });
</script>

C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace TestProject
{
    /// <summary>
    /// Summary description for myService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class myService : System.Web.Services.WebService
    {

        [WebMethod]
        public string SerializeJson(Person person)
        {
            return "Success";
        }

        public class Person
        {
            public string firstName { get; set; }
            public string lastName { get; set; }
            public string department { get; set; }
            public Address address { get; set; }
            public string[] technologies { get; set; }
        }

        public class Address
        {
            public string addressline1 { get; set; }
            public string addressline2 { get; set; }
            public string city { get; set; }
            public string state { get; set; }
            public string country { get; set; }
            public string pin { get; set; }            
        }
    }
}

这篇关于JSON Ajax和参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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