WellNo
WellNo

Reputation: 659

use with ajax passed data in my view - Laravel

I have some problems with using data I've passed with ajax. Thats the ajax snippet in my code:

$('a[data-id]').click(function () {
            var id = $(this).attr('data-id');
            var domain = $(this).attr('data-domain');

            $.ajax({
                url: 'getdata',
                type: 'GET',
                dataType: 'json',
                data: {id: id, domain: domain, tld: tld},
                success: function (data) {
                    $('.resultdomain').html(data);
                    console.log(data);
                }
            });
        });

In my Controller is just this:

public function getData(Request $req)
    {
        $getdomain = Domains::where('id', '=', $req['id'])->first();
        return $getdomain;
    }

So if I console.log(data) i'm getting an object with all the data I need. For example ( copy-paste from my console ) :

Object { id: "5", cus_id: "1", name: "hello-from-the-other-site", tld: ".com", ...... } 

thats great but I want to use that also and I couldn't figure out how.

I want to print the domain-name + the tld. and some other things out.

something like:

Domain: ( here I want to be the name + the tld together ). Created-data : ( date ) Customer-ID = ( cus_id ).

Thanks for any help and sorry for my bad english :-)


current ajax code:

 $('a[data-id]').click(function () {
            var id = $(this).attr('data-id');
            var domain = $(this).attr('data-domain');

            $.ajax({
                url: 'getdata',
                type: 'GET',
                dataType: 'json',
                data: {id: id, domain: domain, tld: tld},
                success: function (data) {
                    var domain = data.name + data.tld;
                    $('.resultdomain').html(domain);
                }
            });
        });

in my view:

Domain: <div class="resultdomain"></div>

Upvotes: 0

Views: 49

Answers (2)

eisbehr
eisbehr

Reputation: 12452

Your data is an object. You can access the properties directly and create the strings you want to have. So in your like success callback you can do it like this:

var data = {
    id: "5",
    cus_id: "1", 
    name: "hello-from-the-other-site", 
    tld: ".com"
};

var domain = data.name + data.tld;
$('.resultdomain').html(domain);
console.log("My Domain: " + domain);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="resultdomain"></div>

Upvotes: 1

Jason
Jason

Reputation: 336

In your JS code, the success function would need something like the following:

success: function (data) {
  $('.resultdomain').html(data.name + data.tld);
}

Upvotes: 0

Related Questions