2

json object sent with xmlhttprequest need to be saved on server. I see "200 ok" from Firebug. But I have 3 problems:

  1. server side scripting/.cs didn't run
  2. how can I save the data posted
  3. the response/responseText is the whole page .aspx , how can I change it?

Thank you. Please refer to the following code:

.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) 
{    }
}

helper class is missing in program.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Michael
  • 539
  • 10
  • 19

1 Answers1

1

You may also use Fiddler and Firebug debug XMLHttpRequest Post.

Firebug

When a request is made to the server via a XMLHttpRequest object, Firebug logs the POST or GET request, the response headers and the raw text of the response. To view this data, click on the Net Tab’s XHR sub-tab. This will show a list of the calls and the time it took for the response. To the left of the request click on the + or simply click on the request (it is a link). Three tabs will appear in the case of a GET request, four for a POST:

Params: Displays the name/value pairs of the request URL

Headers: Displays requestion and response headers

Response: Shows the actual data received from the server as it was received.

Post. Displays the data sent to the server from a POST request (tab only shows for POST requests, not GET requests).

These four tabs are useful in development and debugging. Check the POST and Params tabs to ensure your request are being correctly posted. Check the Response to determine the format of the response and ensure that your JavaScripts are written to handle that formatting. If you don’t control the feed that you are fetching, you can copy and paste the response into a text editor, format it so it’s human legible, and work that way.

Fiddler:

Fiddler supports the notion of breakpoints. When the Enable Single Step Debugging option is checked on the Rules menu, or when the properties of the HTTP Request or Response match the target criteria, Fiddler can pause HTTP traffic and allow edits. This feature proves useful for security testing, as well as for general functionality testing, because all code paths can be exercised.

Megadotnet
  • 309
  • 2
  • 6