user3840485
user3840485

Reputation: 5075

return object get into a variable

I need to get object array into a variable which is return from controller,to the success function in the ajax function,I need to concatenate id to the site_url,

here is my code

view

<div id="abc"></div>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script>   //no need to specify the language
       $(document).ready(function() {

       $('#myForm1').on("submit",function(e) {

            //var form = $(this);
            //dataString = $("#myForm1").serialize();
            e.preventDefault();

            $.ajax({
                type: "POST",
                url: "<?php echo site_url('form_controller/insert_into_db'); ?>",
                data: $(this).serialize(),
                //dataType: "html",
                success: function(data){
                   // top.location.href = "<?php echo site_url('form_controller/callform'); ?>";
                    //$.each(data.results, function(){
                   // $("#abc").append('<div><b>' + id.id + '</b></div><hr />');

                    //});
                    /*var site_url = "<?php// echo site_url('form_controller/callform/') ?>";
                    site_url = site_url +"/" + id;
                    $("#abc").load(site_url);*/
                    <?php //foreach(): ?>

                    var site_url = "<?php echo site_url('form_controller/callform'); ?>";
                    var mydata=window.JSON.stringify(data.trim());


                    site_url = site_url +"/" + "mydata" ;
                    //alert(mydata);
                     $("#abc").load(site_url);
                    $('#abc').html(data);
                    alert(data);

                }//,
                //error: function() { alert("Error posting feed."); }
           });
        });
        });
    </script> 

here is my alert output

here I need to concatenate data as id for the site_url. here is my alert

[{"id":"215"}]

I need to get this as :

var id=215;

Upvotes: 1

Views: 177

Answers (2)

Kyle Emmanuel
Kyle Emmanuel

Reputation: 2221

Set datatype to "json" and use data.id

dataType

Type: String
The type of data expected from the server. 
Default: Intelligent Guess (xml, json, script, text, html).

Upvotes: 1

SlyBeaver
SlyBeaver

Reputation: 1312

You can use JQuery for a parse JSON.

...
 success: function(data){
                  ...
                    var site_url = "<?php echo site_url('form_controller/callform'); ?>";
                    var mydata=window.JSON.stringify(data.trim());

                    var jobj=jQuery.parseJSON(data.responseText); //<---JSON to object
                    site_url = site_url +"/" + jobj.id ; //<--- jobj.id - your id
....

Upvotes: 2

Related Questions