Jquery&请求参数

时间:2011-09-03 20:26:22

标签: jquery asp.net

以下方法我通过jQuery调用我的pageload函数。

请告诉我如何传递查询字符串。

<script type="text/javascript" src="script/jquery-1.3.2.min.js"></script>

<script type="text/javascript">
  $(document).ready(function() {
    $('#Customers').change(function() {
      $.ajax({
        contentType: "text/html; charset=utf-8",
        data: "CustomerID=" + $('#Customers').val(),
        url: "FetchCustomer.aspx",
        dataType: "html",
        success: function(data) {
          $("#CustomerDetails").html(data);
        }
      });
    });
  });
</script>

我有几个问题,比如

  1. 当键入:“POST”,然后contentType:“application / json; charset = utf-8”, 必须是application / json?它不能是HTML?

  2. 当键入:“POST”时,url不能像url:“Customer.aspx?ID = 101 / FetchCustomer” 我的意思是我不能再传递查询字符串。

  3. 请指导我。感谢

2 个答案:

答案 0 :(得分:1)

GETPOSTcontentType无关,您可以使用contentType:"html"

设置type:"POST"

当您设置type:"POST"时,表单值在查询字符串中不可见,如www.utopia.com?name=john&lastName=smith

The HTML specifications technically define the difference between "GET" and "POST" so that


former means that form data is to be encoded (by a browser) into a URL while the latter means that 

the form data is to appear within a message body. But the specifications also give the usage 

recommendation that the "GET" method should be used when the form processing is "idempotent", and in

 those cases only. As a simplification, we might say that "GET" is basically for just getting 

(retrieving) data whereas "POST" may involve anything, like storing or updating data, or ordering a 

product, or sending E-mail.

参考:http://www.cs.tut.fi/~jkorpela/forms/methods.html#fund

  $(document).ready(function() {
    $('#Customers').change(function() {
      $.ajax({
        type:'POST',
        contentType: "text/html; charset=utf-8",
        data:{CustomerID:$('#Customers').val()},
        url: "FetchCustomer.aspx",
        dataType: "html",
        success: function(data) {
          $("#CustomerDetails").html(data);
        }
      });
    });
  });

答案 1 :(得分:1)

您只需删除内容类型并指定类型

即可
  $.ajax({
    type:"POST",
    data: "CustomerID=" + $('#Customers').val(),
    url: "FetchCustomer.aspx",
    success: function(data) {
        $("#CustomerDetails").html(data);
    }
  });
相关问题