2013-08-05 64 views
0

我有一个简单的查找表单,从asp服务器获取数据。用户提交表单后,表格会在同一页面上更新。我试图将查找转换为使用Ajax,以便只重新加载表而不是整个页面。但我如何传递一个asp变量作为数据值传递给服务器?以及我如何解析从asp服务器返回的数据?我目前如何设置我没有得到任何回应。如果我硬编码的数据值,并提醒“测试”阿贾克斯调用工程..任何帮助的noob将不胜感激!如何用jQuery ajax解析asp响应?

getinfo.asp

<form name="form" method="get" action="getinfo.asp"> 
    <input id="appendedInputButton" name="txtsearch" value="<%=txtSearch%>" type="text"> 
    <button id="submitform" type="submit" onclick="event.preventDefault();" >Search</button> 
</form> 

<div id="showresults"> 
<table> 
    <tr> 
     <td>Name: <%=name%></td> 
     <td>Email: <%=email%></td> 
     <td>Phone: <%=phone%></td> 
    </tr> 
</table> 
</div> 

    <script> 
     $('#submitform').click(function() { 
      $.ajax({ 
      url: "getinfo.asp", 
      data: { 
       txtsearch: $('#appendedInputButton').val() 
      }, 
      type: "GET", 
      dataType : "html", 
      success: function(html) { 
       $('#showresults').html(html, '#showresults'); 
      }, 
      error: function(xhr, status) { 
       alert("Sorry, there was a problem!"); 
      }, 
      complete: function(xhr, status) { 
       alert("The request is complete!"); 
      } 
      }); 
     }); 
    </script> 

回答

0

我不知道如果<%=名称%>是一个ASP的东西,但你应该提醒这一点:

alert(html); 

因为这就是你的成功的呼叫将它存储到:

success: function(html /* <- This is what you should alert */) { 
    alert(<%=name%>); 
}, 

我只是重新读你的整个问题。你是否也想这样做?

data: { 
       // this gets the value of your input with the id='appendedInputButton' 
    txtsearch: $('#appendedInputButton').val() 
}, 
+0

这些是评论,而不是答案。 – GreatBigBore

+0

这些建议都失败了吗?您的getinfo.asp页面是否真的为您的AJAX生成html代码? – MonkeyZeus

+0

我的不好。我专注于他的问题,而不是仅仅因为他的代码无法工作。所以我不会让我撤销downvote。安慰:无论如何,它使我成为一个声望点。 – GreatBigBore

0

我有我的服务器返回的XML,然后用jQuery解析它:

PHP(你得此转化为ASP,当然):

$dom = new DOMImplementation(); 
$document = $dom->createDocument(); 
$document->formatOutput = true; 
$document->preserveWhitespace = true; 

$functionResult = $document->createElement('FunctionResult'); 
$document->appendChild($functionResult); 

$functionStatus = $document->createElement('FunctionStatus'); 
$functionResult->appendChild($functionStatus); 

$ingredients = $document->createElement('Ingredients'); 
$functionResult->appendChild($ingredients); 

$ingredient = $document->createElement('Ingredient'); 
$ingredients->appendChild($ingredient); 

$success = true; 
$message = 'Request successful; '; 

$functionStatus->setAttribute('function', $function); 
$functionStatus->setAttribute('success', $success); 
$functionStatus->setAttribute('message', addslashes($message)); 

// Now respond to the requestor 
Header("Content-type: text/xml", 1); 
robLog("\nAjax result:\n" . stripslashes($document->saveXML()), false, true); 
echo $document->saveXML(); 

给你的XML像这样:

<?xml version="1.0"?> 
<FunctionResult> 
    <FunctionStatus function="getIngredients" success="1" message="Request successful"/> 
    <Ingredients> 
    <Ingredient> 
    ... 
    </Ingredient> 
    </Ingredients> 
</FunctionResult> 

,你可以分析如下:

$('Ingredient', xml).each(function() { 
    var ingredientDescriptor = {}; 

    $.each(this.attributes, function(i, attribute) { 
     var name = attribute.name; 
     var value = attribute.value; 
     ingredientDescriptor[name] = value; 
    }); 
}