Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
266 views
in Technique[技术] by (71.8m points)

javascript - Difference between $.post and $.ajax?

Curious if anyone knows what the difference is in regards to the data parameter.

I have a $.post method that takes a $('#myform').serialize() as my data param and works.

If I try the same using the $.ajax() approach, it doesn't work as my data param doesn't appear correct.

Does anyone know the difference and what I might use instead of the above .serialize?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This jquery forum thread sums it up:

$.post is a shorthand way of using $.ajax for POST requests, so there isn't a great deal of difference between using the two - they are both made possible using the same underlying code. $.get works on a similar principle.

—addyosmani

In short, this:

$.post( "/ajax", {"data" : json }) 

Is equivalent to the following:

$.ajax({ 
  type: "POST", 
  url: "/ajax", 
  data: {"data": json} 
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...