0

I have the following codes which sends an array to the function /chat in codeigniter

$(document).ready(function () {
    $('#submit').live('click', function (eve) {
        eve.preventDefault();
        $.ajax({
            url: "http://localhost/fq/index.php/splash/chat/",
            type: 'JSON',
            data: a,
            success: function (html) {
                alert(html);
            }
        });
    });

Let us assume that array a contains names of people only. ( John, James, Smith)

I want to be able to retrieve the all the values from the array in the function chat.

How can it be done?

Edit:

I need to retrieve the values from the JSON encoded array in this function (codeigniter)

   public function chat()
{


  //code to retrieve values

  $this->load->view('chat');

}
2
  • Please make an attempt to format your code so that it's easily readable by others. Commented Jun 5, 2012 at 16:34
  • You should consider passing it as JSON. Commented Jun 5, 2012 at 16:36

2 Answers 2

2
  data: a,

should

  data: $('form').serialize(), // 'form' may need to replace by your form selector

But if you want to send only an array like ['John', 'James', 'Smith']... then yours is just fine.

And use dataType: 'json' as configuration if you're expecting Object as response or dataType: 'html' for Html response.

Setting dataType will release you from extra parsing effort.

Sign up to request clarification or add additional context in comments.

1 Comment

The thing is, i need to retrieve the values in the chat function. I edited the original topic. Please check it out.
0

You should do it via JSON, by changing

type: POST

into

type: JSON

Take a look at: http://api.jquery.com/jQuery.getJSON/

Also I agree with thecodeparadox above, it's simply better practice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.