无法通过jQuery Ajax发送POST请求

时间:2020-12-31 20:15:08

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().

我试图发送一个ajax请求作为POST请求。但是当我在firefox的httpFox上验证它时,请求被发送为GET。我尝试了$.ajax()和$.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:

很多人都有相同的查询,并且忽略了$.ajax()中的“type”,但是即使我提到类型为“POST”,请求的类型也是GET。这是我的代码:

$('.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 个解决方案

#1


3  

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.

一个可能的原因可能是,您试图将AJAX请求发送到另一个域:www.testsite.com,而不是托管页面的那个域,这当然是不可能的,而且jQuery试图使用JSONP,而JSONP只适用于HTTP GET。

#1


3  

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.

一个可能的原因可能是,您试图将AJAX请求发送到另一个域:www.testsite.com,而不是托管页面的那个域,这当然是不可能的,而且jQuery试图使用JSONP,而JSONP只适用于HTTP GET。