Welcome Guest, Not a member yet? Register   Sign In
When setting dataType in jQuery i get NOTHING?
#1

[eluser]Sven Delle[/eluser]
I'm trying to pass form data to CI through jQuery AJAX. But whenever I try to actually SET the dataType to json I get NO response from the server.

Code:
$(function(){
$('#form_id').submit(function(event) {
  event.preventDefault();
  
  var form_data = $('#form_id').serialize();

  $.ajax({
   url: 'http://localhost:8080/website/admin/update_record/',
   type: "POST",
   data: { form_data : form_data },
//   data: form_data, // ALSO TRIED THIS
   dataType: 'json', // TOGGLED THIS IN ALL CASES
   success: function(msg)
    {
     alert('Success: ' + msg);
    }
   });
  // prevents from refreshing the page
  return false;
});
});

For testing purposes I simply want to alert what's received on the server.

Code:
var_dump($this->input->post());

Also tried:
Code:
var_dump($this->input->post('form_data'));

I can get post data as array, but whenever I uncomment the dataType : 'json' I get absolute SILENCE from the server.

Now, is that because nothing gets sent to the server?

Or, am I trying to access it the wrong way ($this->input->post() and it ISN'T post data, or?)

Can someone shed some light in this for me. I'm trying to use json as it seems easier (yeah, right - not so far) with updating database.
#2

[eluser]InsiteFX[/eluser]
The best way for test jQuery is with FireFox and the FireBug plugin.

Also try taking the ending slash off of your url!

It woulhelp if you posted your form view code also.
#3

[eluser]Stefan Hueg[/eluser]
jQuery expects to get JSON returned, but you are just outputting a PHP Array. To get this work you will have to encode your output to JSON, like this:

Code:
json_encode(print_r($_POST, true));

Or just change the dataType to text or html.
#4

[eluser]Sven Delle[/eluser]
Thanks. No luck so far. I simply decided to go with the serialized post data. As I've spent all day on this now. And still have problems getting the current CKEditor content to the server - apart from when I click submit a second time.

But I'll create another post for that ...
#5

[eluser]vitoco[/eluser]
First of all, msg var it's a json object, you must use console.log (+firebug) to echo it

Code:
success: function(msg)
    {
     alert('Success:');
     console.log( msg );
    }
   });

Second, json_encode( $array ) doesn't echo anything, just encode the array to json format ( da!! ), so to return do this

Code:
// OPTIONAL JSON HEADERS
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

// $_POST array encoded as JSON
echo json_encode( $_POST ) ;

Saludos




Theme © iAndrew 2016 - Forum software by © MyBB