Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
- <!DOCTYPE html>
- <html lang="en">
- <body>
- <form runat="server">
- <div id="Feedback"></div>
- <div><a href="#" id="ExportButton">Export</a></div>
- <div><a href="#" id="ImportButton">Import</a></div>
- </form>
- <script src="js/jquery-2.1.1.min.js"></script>
- <script>
- function Export() {
- $.ajax({
- type: "POST",
- url: 'default.aspx/GetThings',
- contentType: "application/json; charset=u",
- dataType: "json",
- async: true,
- success: function (result) {
- var json = result.d;
- $('#Feedback').html(json);
- // failed attempt #1
- //localStorage["things"] = json;
- // failed attempt #2
- var things = jQuery.parseJSON(json);
- localStorage["things"] = JSON.stringify(things);
- }
- });
- }
- function Import() {
- $.ajax({
- type: "POST",
- url: "default.aspx/PutThings",
- data: "{'json':'" + localStorage["things"] + "'}",
- contentType: "application/json; charset=u",
- dataType: "json",
- async: true,
- success: function (result) {
- $('#Feedback').html(result.d);
- }
- });
- }
- $(function () {
- $("#ExportButton").click(function () {
- Export();
- });
- $("#ImportButton").click(function () {
- Import();
- });
- });
- </script>
- </body>
- </html>
- using System;
- using System.Collections.Generic;
- using System.Runtime.Serialization;
- using System.Text;
- using System.Web.Script.Serialization;
- using System.Web.Services;
- using System.Web.UI;
- public partial class _default : Page
- {
- [WebMethod]
- public static string GetThings()
- {
- List<Thing> things = new List<Thing>();
- Thing thing = new Thing();
- //thing.Description = "no escaped double quote string works fine";
- thing.Description = "100 (2 1/8") Gear";
- things.Add(thing);
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- StringBuilder json = new StringBuilder();
- serializer.Serialize(things, json);
- return json.ToString();
- }
- [WebMethod]
- public static string PutThings(string json)
- {
- try
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- List<Thing> things = serializer.Deserialize<List<Thing>>(json);
- return things[0].Description;
- }
- catch (Exception e)
- {
- return e.Message;
- }
- }
- public class Thing
- {
- [DataMember]
- public string Description { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement