【发布时间】:2011-12-29 16:35:16
【问题描述】:
使用 xmlhttprequest 发送的 json 对象需要保存在服务器上。我从 Firebug 看到“200 ok”。但我有 3 个问题:
- 服务器端脚本/.cs 没有运行
- 如何保存发布的数据
- response/responseText 是整个页面 .aspx ,如何更改?
谢谢。请参考以下代码:
.aspx:
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="xhr1.aspx.cs" Inherits="ohmy" %>
javascript:
var jsonobject={"time":"10:00am","temparature":"55"};
var data=JSON.stringify(jsonobject);
var url = "xhr1.aspx/savetofile?timeStamp=" + new Date().getTime();
var req = new XMLHttpRequest();
req.onerror = function() {};
req.onreadystatechange = function() {if (req.readyState == 4) {}};
req.open('POST', url, false);
req.setRequestHeader("Content-Type", "application/json");
req.send(data);
.cs:
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ohmy : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static object savetofile(string data)
{ }
}
程序中缺少帮助类。
【问题讨论】:
标签: c# asp.net xmlhttprequest