1

假设您有以下 POST 网址,

url="http://www.example.com/processor?param1=val1&param=val2"
url2="http://www.example.com/processor"

假设您正在使用 Jquery 发送 POST 请求。

$.post(url,{},function(){});

这和,有什么区别

$.post(url2,{"param1":"val1","param2":"val2"},function(){});

你也可以做类似的事情,

$.post(url1,{"param1":"val1","param2":"val2"},function(){});

在 POST URL(如上面的 url(不是 url2))中使用参数是一种好习惯吗?

4

3 回答 3

2

不同之处在于处理后端的变量。

在 url1 中,参数作为 GET 变量发送,在 url2 中,结合 post(),参数作为 POST 变量发送。

我使用组合,因此您可以互换使用它们。一般规则是使用 GET 发送“教学”类型的数据,使用 POST 发送“用户数据”进行数据操作。例如

url="http://www.example.com/look_for_product?product_type=monitors&supplier=dell"

相比于

$.post('http://www.example.com/place_order;,{"product_type":"monitor","supplier":"dell"}.

您也可以使用组合

$.post('http://www.example.com/place_order?product_type=monitors&supplier=dell;,{"colour":"black","quantity":"3"}.
于 2013-10-09T08:37:10.123 回答
1

试试这样:

$.ajax({
    type: 'POST',   
    url: 'http://www.example.com/processor',
    data: { 
        'param1': 'val1', 
        'param2': 'val2' 
    },
    success: function(msg){
        alert('wow' + msg);
    }
})
于 2013-10-09T07:50:21.867 回答
1

It depends on what task you need the page to do:

  • If you create a user registration script, then it is better to use the POST parameters, so they are invisible to the user. (because they are usually many)
  • If you create some search script, then it is better to use the GET parameters, because, you might need later to provide hot-link directly to that search or smomething like this...

Also, If I were you, I would use the jQuery.ajax() it offers a more customizable interface. You can use it like this:

$.ajax({
      type: "POST",
      url: "processor",
      dataType: "json",
      data: { param1: "val1", param2: "val2" }
    }).success(function( receivedValue ) {
         //some code
    }).error(function() {
         //some error handling
    });

As someone else said here, I don't see any reason of combining the GET and POST parameters. This will get things more complicated.

于 2013-10-09T07:42:15.500 回答