IT박스

JQuery Ajax가 POST 대신 GET을 보내고 있습니다.

itboxs 2020. 9. 25. 07:40
반응형

JQuery Ajax가 POST 대신 GET을 보내고 있습니다.


다음 코드는 POST HTTP 요청 대신 GET을 트리거합니다.

function AddToDatabase() {
  this.url = './api/add';
}

AddToDatabase.prototype.postData = function(dataToPost) {
$.ajax({
    type: "POST",
    url: this.url,
    data: dataToPost,
    context: this,
    success: this.onSuccess
  });
};


var AddToDatabase = new AddToDatabase();
data = {data: 'coucou'};
AddToDatabase.postData(data);

왜 그리고 어떻게 POST를받을 수 있습니까?


Google Chrome Inspect 및 Firefox Inspect에서 브라우저가 GET을 보내는 것을 확인합니다. 다음은 Chrome에서 가져온 것입니다.

요청 URL : http : // localhost / SAMPLE-CODES / UPDATE % 20MYSQL / api / add / 요청 방법 : GET 상태 코드 : 200 OK


해결

'./api/add'라는 URL은 실제로 './api/add/index.php'에 게시되었습니다. './api/add /index.php'또는 './api/add /' 를 호출 하면 POST 요청이 제공됩니다.

잘못된 URL 이었지만 어떤 이유로 '.api / add /'에 대한 성공적인 GET 요청을 받았습니다.


MVC에 문제가 있습니다. 어떤 이유로 [HttPost]를 제거하면 ajax에 POST를 사용하도록 지시하더라도 예상대로 작동합니다.

  • 당신이 사용해야한다는 것이 밝혀졌습니다.

유형 : "POST"

  • jQuery 페이지의 예에서

방법 : "POST"

이제 POST입니다.

그러나 문서를 파헤친 후 이것을 발견했습니다.

여기에 이미지 설명 입력


나는이 문제를 가지고 내 htaccess로는 슬래쉬를 제거했기 때문에 FAngle의 제안 @ 당이 - 그리고 내가 URL을 설정했다 /ajax/foo/bar/하지 /ajax/foo/bar. 리디렉션은 요청을 POST에서 GET으로 변경합니다. /를 제거하고 문제가 해결되었습니다!


URL은 './api/add'실제로로 리디렉션되었습니다 './api/add/index.php'. 따라서 리디렉션 후 새 요청이 GET대신 사용하여 보낸이 기괴한 부작용POST

해결책

  • 전체 URL 사용 './api/add/index.php'
  • 또는 슬래시를 추가합니다 './api/add/'.

내 POST가 GET을 보내는 곳에서도이 동작을 발견했습니다. 시나리오는 매우 독특하지만 누군가에게 도움이 될 수도 있습니다.

This was happening to me on my User Role-edit page, where I was using ajax (post) as an immediate action when a role was checked or unchecked.

I also had the server configured to re-authenticate the user (and redirect them) whenever their role information changed so that their claims would be refreshed.

The brutal cycle ended up as:

  1. First Role Update -- POST -- 200 success

  2. Next Role Update -- POST -- 302 Found -> Redirect (I did not notice this until I used Fiddler rather than Chrome's network monitor)

  3. Redirect Call from (2) (Same URL) -- GET -- 404 Not Found (since I only allowed Post)

  4. GOTO (1)

I ended up changing the server to bypass re-authentication/claims update when it detected an ajax request (based on the Accept Types).


I found that when using dataType: 'jsonp' it converts the request to a GET. I changed it to dataType: 'json' it changed from GET to POST.


For me, your piece of code look OK, but if you want to be sure, you can use $.post instead of $.ajax

$.post('ajax/test.html', function(data) {
 $('.result').html(data);
});

jquery link : http://api.jquery.com/jQuery.post/


I had a similar issue and it started working for me as soon as I removed the hardcoded https:// from my url.

jQuery.ajax({
 type: "POST",
 url: "www.someurl.com",//instead of "https://www.someurl.com"
 data: { foo:"bar"},
 success: function(d){ console.log(d); },
 dataType: "JSONP"
});

I had the same problem and found this question but answers didn't solve my problem. I eventually solve it by removing contentType field in ajax request.

contentType: "application/json",

Check out your .htaccess file or search for some other thing that may redirect your request


I had this issue, and it turned out to be a URL Rewrite module in IIS.

ASP.NET MVC 및 WebAPI를 사용하고 있습니다. 소셜 네트워크가 두 개의 다른 페이지로 동일한 URL을 보지 않도록 소문자 URL을 강제하는 규칙을 만들었습니다.

예를 들면 :

" http://url.com/View/Something/123GuidIdSomething "

vs

" http://url.com/view/something/123guididsomething "

그러나 이것은 어떻게 든 내 ajax 요청을 엉망으로 만들었습니다. 규칙을 비활성화하고 문제가 해결되었습니다.

참고 URL : https://stackoverflow.com/questions/12195883/jquery-ajax-is-sending-get-instead-of-post

반응형