XF 2.1 Ajax works with GET, but throws server error with POST

abdfahim

Well-known member
I have a page that outputs JSON from a controller class.

When I try that page with an Ajax request with Type = GET, I get the exact same response as above, as expected.

However, when I send the request with POST, I get 400 Bad request from the server.

I also tried with apps (e.g., RESTed and Postman) to make a simple HTTP POST request, but get the same error. So,seems like I am missing something which is blocking my request.

Any idea, please?

My Ajax code:
JavaScript:
jQuery.ajax({
    type: "POST",
    url: "{{ link('portal-a/reload') }}",  // portal-a/reload is the route to the action class
    dataType:"json",
    success: function(data){
        alert(success);
    },
    error: function(xhr){
        alert(xhr.status + " " + xhr.statusText);
    }
});


Response of simple HTTP Post request (through the app RESTed):

Body:
Code:
Security error occurred. Please press back, refresh the page, and try again.

Header:
Code:
HTTP/1.1 400 Bad Request

Content-Type: text/html; charset=utf-8
Last-Modified: Mon, 09 Sep 2019 23:29:04 GMT
Set-Cookie: mdesklocal_csrf=Mf_xv31W-EicjOzv; path=/
Server: Apache/2.4.18 (Ubuntu)
X-XF-Debug-Stats: {"time":0.3096,"queries":4,"memory":2.93}
Content-Encoding: gzip
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: private, no-cache, max-age=0
Date: Mon, 09 Sep 2019 23:29:04 GMT
Content-Length: 6848
Connection: close
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Vary: Accept-Encoding
 
My Ajax code:
JavaScript:
jQuery.ajax({
type: "POST",
url: "{{ link('portal-a/reload') }}", // portal-a/reload is the route to the action class
dataType:"json",
success: function(data){
alert(success);
},
error: function(xhr){
alert(xhr.status + " " + xhr.statusText);
}
});
Response of simple
That is not how we perform AJAX requests in XenForo JavaScript. That's a key clue.
 
Right, I assumed that. But the only template popped up when I search "XF.ajax" in the XF system is two_step_authy where I was confused with a few things. Hence I fall back to jQuery.

It would have been nice if the official document included a syntax for XF.ajax call.

Thanks anyway.
 
Top Bottom