UPDATE: I Figured it out. Was a URL issue, which redirected and cleared the POST before it was sent to the server.
$('#addbtn').on('click',function() {
$.ajax({
type: "POST",
url: "/create/",
dataType: "json",
data: $('#MultiAdd').serializeArray(),
success: function (data) {
// this returns Failed. Please Try Again. c_name=
// c_name should equal the value from post.
alert(data.msg)
},
error: function (xhr, ajaxOptions, thrownError) {}
});
});
I can not seem to get this to post the data. Ive tried so many variations. Looking at this for hours trying to see whats wrong.
I have tried data: {test:'test'}
(did not work)
the GET function does, but I need this to be POST. I have also tried normal .serialize(). Still didnt work.
This does work to show me the values. but ajax will not POST them in the submission.
console.log($('#MultiAdd').serializeArray());
(on the page, the form and ajax codes are dynamically added, but I dont think that would change much, Ive tested that already.)
Update: (i'm using codeigniter) - Server-side. Trying to access the [input name="c_name"]
public function create()
{
$value = $this->input->post('c_name');
$msg = array("msg"=>"Failed. Please Try Again. c_name=".$value);
die(json_encode($msg));
}
Try adding contentType
to application/x-www-form-urlencoded
$.ajax({
type: "POST",
url: "/create/",
dataType: "json",
data: $('#MultiAdd').serializeArray(),
contentType: "application/x-www-form-urlencoded",
success: function (data) { //success },
error: function (xhr, ajaxOptions, thrownError) { //some sort of error }
});
If GET request works but POST doesn't it's most usually the issue with redirect. That said, while the request initially accessed the first URL and got redirected to another URL then all the POST data were not forwarded along to another URL. Therefore all the data gets lost.
Some common cases that might occur (depending on server configuration):
You get the idea. If you look in network panel and notice that there is no data sent but response was 200 OK then it's good idea to check if Request URL in network panel is the one that you specified in your code. Although this will be possible only in modern browsers. In older browsers you won't even know that URL has been redirected.
For Example (see that "en/" is missing in my request url):