0

I'm New in AJAX.

When passing ID to update product. Any explanation for this. Thank you in advance.

This

$.ajax({
   type: 'post',
   url: 'my_controller/update_product_exe/' + id, //This line
   dataType: 'json'
});

to this...

$.ajax({
   type:'post',
   url: 'my_controller/update_product_exe',
   dataType: 'json',
   data: {id: id} // this line
});
TIGER
  • 2,649
  • 2
  • 30
  • 40
Marky
  • 55
  • 2
  • 8

3 Answers3

4

The difference is the url itself. Appending id to the first url will change it and therefore send the request to that specific url. But, it's not sending any data during request. Example:

// let's say id = "1234"
$.ajax({
   type: 'post',
   url: 'my_controller/update_product_exe/' + id, // This will be 'my_controller/update_product_exe/1234'
   dataType: 'json'
});

And for the second one:

$.ajax({
   type:'post',
   url: 'my_controller/update_product_exe',
   dataType: 'json',
   data: {id: id} // This will be {id: "1234"}
});

On the second one, you are passing data; on the first one, you are just modifying your url by appending some string to it.

Rax Weber
  • 3,551
  • 15
  • 30
3

If you just want to know about the difference in both ajax requests than:

In first request, you are not passing the data in ajax request but sending an ID in URL, in CI controller, you will get this id by using URL Segments.

In Second request, you are sending the data in ajax request, so you can get the data in controller by using $_POST

Now, which one is the better, both of them having difference, when you need to pass some input values using ajax than you can choose second one. You can send multiple data in this request.

You can also use second request for achieving the first request target, in this case you can just pass the ID in ajax data. You can send multiple data but you must need to take of segement URLs.

devpro
  • 15,966
  • 3
  • 25
  • 38
  • I would like to ask, which is better? – Marky Nov 09 '16 at 09:56
  • @Marky: best in the sense of?? what? – devpro Nov 09 '16 at 10:03
  • I was wondering which of that two I would use to update product, because both of them are running properly :( – Marky Nov 09 '16 at 10:03
  • @devpro Isn't the first one modifying the URL? Making it, for example, 'my_controller/update_product_exe/someid' ? Therefore shouldn't it request to another URL? (Unless the server handles it first) – Rax Weber Nov 09 '16 at 10:03
1

Conceptually you are using a GET in the the first example and a POST for the second. HTTP verbs have a meaning and POST is meant to send information to a server. Even if you can get the id by using a GET this does not make it semantically correct. For the moment you only have an id which is limited in size and is only one parameter, but even in a small application one usually sends to a server several parameters and maybe some kb of data. GET parameters are limited in size and POST is better suited for this.

For all this reasons the second version that is using POST is the correct one.

Here are some extra resources on the differences between GET and POST.

http://blog.teamtreehouse.com/the-definitive-guide-to-get-vs-post

http://www.diffen.com/difference/GET-vs-POST-HTTP-Requests

What is the difference between POST and GET?

When should I use GET or POST method? What's the difference between them?

Elzo Valugi
  • 24,234
  • 13
  • 88
  • 112