asp.net中的JSON Web服务 [英] JSON Web service in asp.net

查看:79
本文介绍了asp.net中的JSON Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,



如何在asp.net中创建JSON webservice。



通常之前,我们曾经创建扩展名为.asmx的webservice。



所以,是使用wcf服务或.asmx服务创建的JSON webservice。如何创建JSON web服务。



请举例说明。



谢谢。

good morning,

how to create JSON webservice in asp.net.

normally before, we used to create webservice with extension .asmx.

so, is JSON webservice created with wcf service or .asmx service. how to create JSON webservice.

please explain with an example.

thank you.

推荐答案

本文可能对您有用:关于创建WCF REST服务的初学者教程 [ ^ ]
This article might be useful for you: A Beginner's Tutorial on Creating WCF REST Services[^]


试用此代码



Try This Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.Script.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {  }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public CityList GetCities()
    {
        CityList oBoCityList = new CityList() { new City { Name = "New Delhi", ID = 1 }, new City { Name = "Kanpur", ID = 2 }, new City { Name = "Gurgaon", ID = 3 } };
        return oBoCityList;
    }
}

public class City
{
    public City() { }
    public string Name
    { get; set; }
    public Int32 ID
    { get; set; }
}

public class CityList : List<City>
{
    public CityList() { }
}










<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript" src="Script/jquery-1.7.2.min.js"></script>

    <script language="javascript" type="text/javascript">


.ajax({
type:' POST'
url:' WebService.asmx / GetCities'
data : {}
contentType: application / json; charset = utf-8
dataType: json
错误: function (jqXHR,textStatus,errorThrown){
alert(jqXHR.responseText);
},
成功: function (data){
var cities = data.d;
.ajax({ type: 'POST', url: 'WebService.asmx/GetCities', data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", error: function(jqXHR, textStatus, errorThrown) { alert(jqXHR.responseText); }, success: function(data) { var cities = data.d;


这篇关于asp.net中的JSON Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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