0

I'm trying to start a simple web page in which I wanna load data to a combobox from a request to my php script; The problem that I'm having is that when I load the page it doesn't load, but if i assign the function to a button it does but i have to press it twice for it to start working... I just can't figure out why that's happening, I have the next code in my html:

<div class="form-group">
       <label class="col-md-6 col-xs-12" for="pref-perpage">Categoría:</label>
       <select id="pref-perpage" class="col-lg-9 lindo">
               <option ng-repeat="result in search.vendor">{{result.nombreCategoria}}</option>
               <option selected="selected">[ Seleccione ]</option>
       </select>
</div> <!-- form group [Categoria] -->

this is my javascript:

  search.buscar = function()
  {

    $.ajax({
      url: "Busqueda.php",
      type: "get",
      success: function(response) {
          search.vendor.push({'nombreCategoria':response});
        var JSONObject = JSON.parse(response);

          for(var x in parsed){
          search.vendor.push(parsed[x]);
        }

and the kind of data that I want to fill the array with would be something like:

search.vendor = [{'nombreCategoria':'data'}];

If've also tried the $http method from angular but it does the same; I fill the combobox with fake data manually and I have no problem!

server response: [{"nombreCategoria":"ACCESORIOS"},{"nombreCategoria":"CPU"},{"nombreCategoria":"IMPRESORA"},{"nombreCategoria":"MONITOR"}]

6
  • 1
    Use the $http Anuglar service for get data, not $.ajax Commented Apr 15, 2016 at 3:12
  • Yeah, as i wrote before i already tried that...and worked the same...still having the same problem; actually that's "old", i'm using the $http right now and gives me the next error: SyntaxError: Unexpected token o, in the line of the JSON.parse... Commented Apr 15, 2016 at 3:15
  • could you give us an example server response? Commented Apr 15, 2016 at 3:21
  • Yes, of course, the server response to that is: [{"nombreCategoria":"ACCESORIOS"},{"nombreCategoria":"CPU"},{"nombreCategoria":"IMPRESORA"},{"nombreCategoria":"MONITOR"}] Commented Apr 15, 2016 at 3:24
  • try replacing search.vendor.push() with $scope.search.vendor.push() Commented Apr 15, 2016 at 4:44

1 Answer 1

0

I'm working with Gabriel in this project. The soluction was to add the method $scope.$apply(); after the .push() method...

So, the function looks like this:

search.buscar = function() {
    $.ajax({
        url: "Busqueda.php",
        type: "get",
        success: function(response) {
            search.vendor.push({'nombreCategoria':response});
            var JSONObject = JSON.parse(response);
            for(var x in parsed) {
                search.vendor.push(parsed[x]);
                $scope.$apply();
            }
        }
    });
}

By the way... I found the solution here (stackoverflow)

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.