C# asp.net中转换的php代码未按预期工作?
在执行Ajax的示例时,用户在textbox中键入一个文本,并使用javascript连续地发回其文本,同时显示结果,无论该项是否可用 mainhtml外观如下C# asp.net中转换的php代码未按预期工作?,c#,javascript,php,asp.net,ajax,C#,Javascript,Php,Asp.net,Ajax,在执行Ajax的示例时,用户在textbox中键入一个文本,并使用javascript连续地发回其文本,同时显示结果,无论该项是否可用 mainhtml外观如下 <body> <h3>The chuff Bucket </h3> Enter the food you like to order: <input type="text" id="userInput" onfocus="
<body>
<h3>The chuff Bucket </h3>
Enter the food you like to order:
<input type="text" id="userInput" onfocus="process()"/>
<div id="underInput"/>
</body>
粗水桶
输入您想要点的食物:
下面是我的javascript代码
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject() {
var xmlHttp;
if (window.ActiveXObject) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xmlHttp = false;
}
} else {
try {
xmlHttp = new XMLHttpRequest();
}
catch (e) {
xmlHttp = false;
}
}
if (!xmlHttp) {
alert("cant create that object boss!");
} else
return xmlHttp;
}
function process() {
if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
food = encodeURIComponent(document.getElementById("userInput").value);
xmlHttp.open("GET", "foodstore.aspx?food=" + food, true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
} else {
setTimeout('process()', 1000);
}
}
function handleServerResponse() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
message = xmlDocumentElement.firstChild.data;
document.getElementById("underInput").innerHTML = '<span style = "color:blue;">' + message + '</span>';
setTimeout('process()', 1000);
} else {
alert('Something went wrong');
}
}
}
var xmlHttp=createXmlHttpRequestObject();
函数createXmlHttpRequestObject(){
var-xmlHttp;
if(window.ActiveXObject){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}
捕获(e){
xmlHttp=false;
}
}否则{
试一试{
xmlHttp=新的XMLHttpRequest();
}
捕获(e){
xmlHttp=false;
}
}
如果(!xmlHttp){
警报(“无法创建对象boss!”);
}否则
返回xmlHttp;
}
函数过程(){
if(xmlHttp.readyState==0 | | xmlHttp.readyState==4){
food=encodeURIComponent(document.getElementById(“userInput”).value);
open(“GET”,“foodstore.aspx?food=“+food,true”);
xmlHttp.onreadystatechange=handleServerResponse;
xmlHttp.send(空);
}否则{
setTimeout('process()',1000);
}
}
函数handleServerResponse(){
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
xmlResponse=xmlHttp.responseXML;
xmlDocumentElement=xmlResponse.documentElement;
message=xmlDocumentElement.firstChild.data;
document.getElementById(“underInput”).innerHTML=''+消息+'';
setTimeout('process()',1000);
}否则{
警惕(“出了什么事”);
}
}
}
php的代码运行良好,这是
<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$food = $_GET['food'];
$foodArray = array("tuna","bacon","beef","mutton","biryani");
if($food == '')
echo 'Enter a food you idiot.';
else{
if(in_array($food,$foodArray))
echo 'We do have '.$food.'!';
else
echo 'We do not sell '.$food.'!';
}
echo '</response>';
?>
但是当转换到asp.net时,它是回发和工作的,但没有显示分区内的reult。主要原因是我找出了相关的回音线
protected void Page_Load(object sender, EventArgs e)
{
Response.ContentType = "text/xml";
System.Diagnostics.Debug.Print("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>");
System.Diagnostics.Debug.Print("<response>");
string food = (string)Request.QueryString["food"];
string[] foodArray = new string[] { "tuna", "beef", "mutton", "fish","biryani" };
if (String.IsNullOrEmpty(food))
{
System.Diagnostics.Debug.Print("Enter a food you idiot.");
}
else
{
if (foodArray.Contains(food))
{
System.Diagnostics.Debug.Print("We serve " + food);
}
else
{
System.Diagnostics.Debug.Print("We do not serve " + food);
}
}
System.Diagnostics.Debug.Print("</response>");
}
受保护的无效页面加载(对象发送方,事件参数e)
{
Response.ContentType=“text/xml”;
System.Diagnostics.Debug.Print(“”);
System.Diagnostics.Debug.Print(“”);
string food=(string)Request.QueryString[“food”];
字符串[]foodArray=新字符串[]{“金枪鱼”、“牛肉”、“羊肉”、“鱼”、“比里亚尼”};
if(String.IsNullOrEmpty(食物))
{
System.Diagnostics.Debug.Print(“输入食物,你这个白痴”);
}
其他的
{
如果(食品中含有(食品))
{
系统。诊断。调试。打印(“我们提供”+食物);
}
其他的
{
System.Diagnostics.Debug.Print(“我们不提供”+食品);
}
}
System.Diagnostics.Debug.Print(“”);
}
注意:我已尝试而不是打印
响应。写入,但结果与id=underInput的div相同,没有得到结果。这里有人帮忙吗 确保已将所有System.Diagnostics.Debug.Print替换为Response.Write。然后,检查foodstore.aspx上的HTML标记
Foodstore.aspx应该有@Page指令,但没有其他指令。。您希望返回XML内容,因此需要确保页面上没有DOCTYPE或HTML元素