0

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

获取信息.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>
4

2 回答 2

0

我不确定 <%=name%> 是否是 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()
},
于 2013-08-05T16:01:12.720 回答
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;
    });
}
于 2013-08-05T16:09:46.267 回答