AJAX 后台怎么取不到直
代码如下 请高手 指点下 谢谢
ajax_http.js
-------------
function getXMLHTTP() {
var xmlhttp;
if (window.ActiveXObject) {
//IE
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlhttp = new ActiveXObject("Msxml2.XMLHTP");
}
} else {
//FireFox
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
var XMLHttpObj;
function addTestInfo1() {
XMLHttpObj = getXMLHTTP();
// var subName = document.getElementById("D1").value;
var txtTitle = document.getElementById("txt").value;
var txtA = document.getElementById("txtA").value;
var txtB = document.getElementById("txtB").value;
var txtC = document.getElementById("txtC").value;
var txtD = document.getElementById("txtD").value;
var txtQuestion = document.getElementById("txt1").value;
var txtFenS = document.getElementById("txt2").value;
XMLHttpObj.open("POST", "checkTestInfo.aspx?TestTitle=" + txtTitle + "&TestA=" + txtA + "&TestB=" + txtB + "&TestC=" + txtC + "&TestD=" + txtD + "&TestAnswer=" + txtQuestion + "&TestFenShu=" + txtFenS, true);
XMLHttpObj.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
XMLHttpObj.onreadystatechange = proc;
XMLHttpObj.send(null);
}
function proc() {
if (XMLHttpObj.readyState == 4) {
if (XMLHttpObj.status == 200) {
// var v = XMLHttpObj.responseText;
//alert(v);
// var vv = XMLHttpObj.responseXML;
//javascript解析XML
//var ret = vv.documentElement.firstChild.data;
//alert(ret);
if (XMLHttpObj.responseText =="true") {
window.alert("添加成功");
}
else {
window.alert("添加失败");
}
}
}
}
---------------------
添加界面
addTestInfo.aspx
--------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="addTestInfo.aspx.cs" Inherits="Admin_addTestInfo" %>
<!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="js/ajax_http.js"></script>
<style type="text/css">
#Select1
{
width: 74px;
}
#Text1
{
height: 46px;
width: 191px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
学科<select id="Select1" name="D1" runat="server">
<option></option>
</select><br />
<br />
题干:<input id="txtTitle" type="text"name="txt"/><br />
<br />
选项A:<input id="txtA" type="text" name="txtA"/><br />
<br />
选项B:<input id="txtB" type="text"name="txtB" /><br />
<br />
选项C:<input id="txtC" type="text" name="txtC"/><br />
<br />
选项D:<input id="txtD" type="text" name="txtD"/><br />
<br />
正确答案:<input id="txtQuestion" type="text" name="txt1"/><br />
<br />
分 数:<input id="txtFenS" type="text" name="txt2" /><br />
<br />
<input id="Submit1" type="submit" value="添加" onclick="addTestInfo1();" /></div>
</form>
</body>
</html>
-------------------
连接数据界面
--------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using DAL;
using BLL;
using Model;
public partial class Admin_checkTestInfo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string txtTitle = Request.QueryString["TestTitle"];
string txtA = Request.QueryString["txtA"];
string txtB = Request.QueryString["txtB"];
string txtC = Request.QueryString["txtC"];
string txtD = Request.QueryString["txtD"];
string txtQuestion = Request.QueryString["txtQuestion"];
string txtFenS = Request.QueryString["txtFenShu"];
//context.Response.ContentType = "text/xml";
DBcon db = new DBcon();
SqlConnection con = db.GetCon();
string sql = "insert into Test(TestTitle,TestA,TestB,TestC,TestD,TestQ,TestFenS) values('" + txtTitle + "','" + txtA + "','" + txtB + "','" + txtC + "','" + txtD + "','" + txtQuestion + "','" + txtFenS + "')";
int i = db.GetOperation(sql);
}
}
}