views:

124

answers:

2

Hi,

I was trying to send an ajax request as POST request. But when i verified it on httpFox on firefox, the request is sent as GET. I tried both $.ajax() and $.post().

Many had a query regarding the same and had missed the "type" in $.ajax(), but even if i mention the type as "POST", the request will be of type GET. Here is my code:

$('.test').click(function(){
   alert("clicked");
   $.ajax({
   type: "POST",
   url: "www.testsite.com",
   data: "name=John&location=Boston",
success: function(msg){
    alert( "Data Saved: " + msg );
}
});
 });

Any idea why it happens?

+1  A: 

A possible cause might be the fact that you are trying to send an AJAX request to a different domain: www.testsite.com than the one hosting your page which of course is not possible and jQuery tries to use JSONP instead which works only with HTTP GET.

Darin Dimitrov
Thanks Darin. I was trying with absolute path. When changed it to relative path, it worked.
Amit
A: 

What does firebug tell you about the request method used? If its neither GET, nor POST but OPTIONS then its probably because you're posting data to a domain other than the one from which the script comes.

If the service on that "other" domain allows JSON-in-script or similar alternate, you can still use them via jQuery.

Salman A