如何使用jQuery ajax解析asp响应?

时间:2013-08-05 15:57:15

标签: jquery ajax asp-classic get

我有一个简单的查找表单,可以从asp服务器获取数据。用户提交表单后,将在同一页面上更新表。我正在尝试将查找转换为使用ajax,以便只重新加载表而不是整个页面。但是如何将asp变量作为数据值传递给服务器呢?以及如何实际解析从asp服务器返回的数据?我目前如何设置我没有得到回应。如果我对数据值进行硬编码,并且警告“测试”,那么ajax调用就可以工作..对于菜鸟的任何帮助都将不胜感激!

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>

2 个答案:

答案 0 :(得分:0)

我不确定&lt;%= name%&gt;是一个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()
},

答案 1 :(得分: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;
    });
}