0

I am currently in a bind, JQuery is unable to parse the following json strings

{ "query":"Unit", 
"suggestions": 
[ {"value":"Mr Ruto Kimutai ","data":88},{"value":"Mr Kimani Karanja","data":79} ] } 

{"query":"Unit",
"suggestions":
[{"value":"Mr Ruto Kimutai ","data":88},{"value":"Mr Kimani Karanja","data":79}]}

The above strings when parse through JSON.parse create the following arror:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 112 of the JSON data

The PHP code which creates the string above is this:

public function getCustomerSuggestions($name){

        $customers = $this->model->where('name','LIKE','%'.$name.'%')->show();

        if(count($customers)>=1){

            foreach($customers as $customer){
                $list[] = ['value' => ucfirst($customer->name),'data' => $customer->id];
            }
        }
        else{

            $list[] = ['value' => 'No Customers Found', 'data'=> NULL];
        }

        $full_list['query'] = 'Unit';
        $full_list['suggestions'] = $list;

        return json_encode($full_list);
    }

As you can see I am using the function json_encode to create the JSOn string so there should be no issue but it still doesnt work.

Edit The json is sent using an autocomplete tool called DevBridge Autocomplete which takes the JSON strings and creates a suggestion list. The code I am using is

$('input[name=\"customer\"]').devbridgeAutocomplete({
                serviceUrl: '".SITE_PATH."/ajax/admin/quotes/getcustomer',
                minChars: 1,
                onSearchStart: function (query){
                    var searchinput = $(this).val();
                    $('.autocomplete-suggestions').html('Searching: '+searchinput);                   
                },
                onSelect: function(suggestion){   
                    var selection = $(this).val(suggestion.value); 
                    $('input[name=\"customerid\"]').val(suggestion.data);
                    $.get('".SITE_PATH."/ajax/admin/quotes/getcustomerdetails',{id: suggestion.data},
                        function(response){   
                            var obj = $.parseJSON(response);
                            $.each(obj, function(key, value){
                                $('#'+key).val(value);
                            });
                        });
                }
            });
2
  • Please add code of jquery Commented Jun 2, 2016 at 17:54
  • are they two separate strings or one ? Commented Jun 2, 2016 at 18:10

2 Answers 2

3

It seems you have two JSON objects after each other. That's simply invalid. There can only be a single value at the root of a JSON "document". If you want to send down multiple objects, you need to put them in an array.

It seems getCustomerSuggestions is called multiple times and the return value of each call is returned to the client. Instead, the method should return an array, the caller should collect the return values in an array and JSON encode that array.

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

3 Comments

The problem is the code generates a list on each progressive keystroke. So each list is generated dynamically
So? I assume each keystroke is a separate request or is it not? The only thing that matters is that the response of each request is valid.
Ok let me trace how each response is made
0

Well, your JSON string is NOT valid. It should be,

[
  { "query":"Unit", 
  "suggestions": 
  [ {"value":"Mr Ruto Kimutai ","data":88},{"value":"Mr Kimani Karanja","data":79} ] } 
  ,
  {"query":"Unit",
  "suggestions":
  [{"value":"Mr Ruto Kimutai ","data":88},{"value":"Mr Kimani Karanja","data":79}]}
]

But as Felix Kling said, check your PHP code.

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.