Welcome Guest, Not a member yet? Register   Sign In
AJAX Post Request does not work
#1

Hey guys,
I'm new here. I have used CI over the past 10 years and love it. Unfortunately, I'm not getting the hang with everything CI4.
So, I have created a form and when I submit it with AJAX (POST), I am getting no result. After further examinations I have come to realize that no data is passed when I execute a Ajax-Request with the type POST.

PHP Code:
public function index() 
PHP Code:

PHP Code:
$view["head_title"] = 'Login' TITLE
PHP Code:
$view["nav_title"] = TITLE
PHP Code:
// $js = 'get("#loginBox", "/login/test2/", "");'; 
PHP Code:
$js 'var xhr = $.ajax({ 
PHP Code:
url"/login/test/"
PHP Code:
type"POST"
PHP Code:
dataType"html"
PHP Code:
// data: "&test=test", 
PHP Code:
data: { name"John"location"Boston" }, 
PHP Code:
success: function(result) { alert(result); }, 
PHP Code:
error: function(resultstatuserror) { alert("result" result ", status" status ", error" error); }, 
PHP Code:
});'; 
PHP Code:
$view["js"] = $this->BrainsHTML->js($js); 
PHP Code:
$this->Output->get_views($view, array('login')); 
PHP Code:
}
 
 echo 
view('show', array('show' => $show));
 }
 
 
 public function 
test()
 {
 echo 
csrf_hash();
 echo 
$this->request->isAJAX();
// print_r($this->request->headers());
 
echo $_POST["test"];
// print_r($_GET);
 
print_r($this->request->getPost());
// print_r($this->request->getJSON());
// print_r($this->request->getJsonVar());
 
print_r($this->request->getGet());
 
print_r($this->request->getMethod());
 } 


The alert shows the following:
4c352b93ef0c5b27c340fe27f2db69c11Array
(
)
Array
(
)
get


I have been on this for hours now and I am out of ideas. Please help me and let me know if you need more infos about the setup. From where I stand it should be pretty simple, as I have never had an issue with this code in CI3.

Thanks,
Alain
Reply
#2

I finally found the solution.

The Rewrite directives in .htaccess were not set properly. Sorry about that.
Reply
#3

first question does it have to be ajax ?
CMS CI4     I use Arch Linux by the way 




Arch Book
Reply
#4

I always like to get my forms working first then switch them over to Ajax.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB