0

I am working in php codeigniter and I tried like this in my code.

var dataSource = $.ajax({
            url: <?php echo $url=$this->BASE_URL."home/destination_place" ?>,
            method: "POST",
            data: { id : id,firstName:firstName },
            dataType: "html"
        });

This is my returning array but it is not getting in the data source field

[{"id":"1","firstName":"dubai"},{"id":"2","firstName":"munnar"},{"id":"3","firstName":"wayanad"},{"id":"4","firstName":"kovalam"}]

I hope somebody will help me to solve this. hopefully

Muhammed Raheez PC
  • 393
  • 2
  • 4
  • 19
  • 3
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Adam Azad Feb 21 '18 at 05:13
  • 1
    `This is my returning array` - you don't show any code that deals with your "returning array" - all that code does is post and ignore the response – Jaromanda X Feb 21 '18 at 05:13
  • @JaromandaX didn't get you – Muhammed Raheez PC Feb 21 '18 at 05:15
  • you say `This is my returning array` ... what does this even mean? returning array? returning from what? there's no array in your code, so where does this returning array return from – Jaromanda X Feb 21 '18 at 05:18
  • that url return this array – Muhammed Raheez PC Feb 21 '18 at 05:20
  • urls don't return arrays by default... this function just posts to 'home/destination_place' and doesn't do anything else. the success or done function is where you can get the data... – Alex Feb 21 '18 at 06:23

2 Answers2

1

I tend not to use .ajax because it's bulky and messy. The .get() and .post() methods work well and are nice and clean.

var dataSource = '';
$.post('<?= $this->BASE_URL.'home/destination_place' ?>',
        { id: id, firstName: firstName },
        function(resp){ dataSource = resp; });

The third argument is the callback function, which takes the response as a function parameter. You can then assign it to a variable outside the callback.

Something to bear in mind is that AJAX is asynchronous. The response value will not be available until the call to the remote machine completes. For this reason, all data processing should be triggered inside the callback function, where in this case we are assigning resp to dataSource.

sorak
  • 2,607
  • 2
  • 16
  • 24
  • And this works for you? Because I tried it and it doesn't for me. Empty response if I `console.log(dataSource)` – Alex Feb 21 '18 at 06:41
  • Yes, this prototype works for me just fine. I use jQuery like this very frequently, personally and professionally. Just make sure the correct URL is ending up in the first parameter. – sorak Feb 21 '18 at 06:45
  • URL is correct,verified in network pane of dev tools, post data also verified set. still a console.log after the function returns no results despite having a clear response in the response pane. dubious... Results are json btw. – Alex Feb 21 '18 at 06:47
  • "dubious"? Here's an example you can run in your console... `$.get('google.com', function(resp){ console.log(resp); });` ... this stuff is very simple usage of jQuery. Read up on it. – sorak Feb 21 '18 at 06:52
  • it's asynchronous. when you're trying to print it, it has not yet returned. – sorak Feb 21 '18 at 07:02
  • yea thats the nature of ajax unless async is false, which isn't advised, but won't that mean that the variable won't be useful when needed as the user intends (as a variable presumably to be used with other jquery functions)? – Alex Feb 21 '18 at 07:03
  • it's just something to be aware of. the necessary processing logic should be launched by the callback function so the value is available. – sorak Feb 21 '18 at 07:04
  • yea that would be good to mention in your answer as it can be confusing – Alex Feb 21 '18 at 07:09
0

dataType parameter is the type of data you're expecting to get from server. If I undearstand correctly, you want to get JSON from server, but u specified dataType as html. Try:

var dataSource = $.ajax({
            url: <?php echo $url=$this->BASE_URL."home/destination_place" ?>,
            method: "POST",
            dataType: "json",
            data: { id : id,firstName:firstName },
        });
Piotr K.
  • 106
  • 1
  • 4
  • Believe the user is trying to populate the variable with the response. This won't work for that. – Alex Feb 21 '18 at 06:34