1

Lets say you have following urls for POST,

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

Say you are sending POST request using Jquery.

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

How is that different from,

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

Can you also do something like,

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

Is is a good practice to use parameters in POST URL (like url above (not url2))?

specialscope
  • 4,188
  • 3
  • 24
  • 22
  • Read this http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them – Rohan Kumar Oct 09 '13 at 07:39
  • This question is a direct duplicate of http://stackoverflow.com/questions/504947/when-should-i-use-get-or-post-method-whats-the-difference-between-them –  Oct 09 '13 at 07:40
  • 2
    Why would you ever want to combine a get and a post? They're used for different things. – Johan Oct 09 '13 at 07:41
  • I am not combining GET and POST. I am currently looking through a web application and I found this strange usage of POST that I had never seen before. It contains parameters AND POST body. – specialscope Oct 09 '13 at 07:45

3 Answers3

2

The difference is in handling the variables on the backend.

In url1, the parameters are sent as GET variables, and in url2, combined with the post(), the parameters are sent as POST variables.

I use a combination, so you can use them interchangably. The general rule is to use GET to send 'instructional' type data, and to send POST to send 'user data' for data manipulation. For example

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

Compare to

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

You can also use a combination

$.post('http://www.example.com/place_order?product_type=monitors&supplier=dell;,{"colour":"black","quantity":"3"}.
crafter
  • 6,246
  • 1
  • 34
  • 46
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.

Victor
  • 13,914
  • 19
  • 78
  • 147
  • I think you did not quite get my question. I am well aware of usage of GET and POST. – specialscope Oct 09 '13 at 07:50
  • Plz look at my question, I know how to use GET and POST in jQuery. My question is, is it a good idea to use "http://www.example.com/processor?param1=val1&param=val2" instead of adding them as JSON parameters like you did above? – specialscope Oct 09 '13 at 07:55
  • I think it is a better way to add your variables as JSON parameters, because this is more readable for you. I am not sure about the speed of both scripts, but if you need I can test them with Firebug. – Victor Oct 09 '13 at 07:58
1

Try like this:

$.ajax({
    type: 'POST',   
    url: 'http://www.example.com/processor',
    data: { 
        'param1': 'val1', 
        'param2': 'val2' 
    },
    success: function(msg){
        alert('wow' + msg);
    }
})
Victor
  • 13,914
  • 19
  • 78
  • 147
Satyam Saxena
  • 581
  • 2
  • 10