0

I have jquery ajax call which will go to a .js file and returns JSON

var validate = function () {
    alert('test string');
}

$.ajax({
        url: "JS/JQB/SampleData/Filters.js",
        async:false,
        success: InitailiseJQB
    });

Filters.js file contains below JSON,

  [{
        "id": "Group",
        "filters": [{
            "id": "49be7d78-4dcf-38ab-3733-b4108701f1",            
            "validation": {
                "callback": validate
            }
        }]
}]

in the above JSON validate is the javascript function but the problem is when i am requesting for the JSON using ajax call, it is throwing run time error called JavaScript runtime error: Invalid character

Vallabha
  • 1,591
  • 3
  • 13
  • 21
  • 2
    That's not valid JSON. The right-hand side of a JSON property clause can be a string, a number, a JSON array, a JSON object, `true`, `false`, or `null`. – Pointy Aug 06 '15 at 18:38
  • @Pointy but if i remove the ajax call and put it in a variable then it is replacing the validate with the function I have given, and every thing working fine. – Vallabha Aug 06 '15 at 18:41
  • You could add the parameter datatype: "JSON" as you are requesting JSON from your script – Fintan Creaven Aug 06 '15 at 19:08
  • Try your script with some known valid JSON and see if it works. You will find plenty on here. – Fintan Creaven Aug 06 '15 at 19:17
  • @FintanCreaven yes it is working for valid JSON – Vallabha Aug 07 '15 at 02:00
  • @FintanCreaven datatype:"json" is also not working. facing same error – Vallabha Aug 07 '15 at 02:01
  • @user1655222 Variables and functions are simply not valid in JSON. The snippet may "work" if it's parsed as JavaScript instead, as "*put it in a variable*" suggest you've done. You'll have to find a different approach to reference the function from valid JSON data. – Jonathan Lonowski Aug 07 '15 at 02:16
  • @JonathanLonowski yes. I have put in a variable and it is working fine but i googled it but didn't find any solution to do the things – Vallabha Aug 07 '15 at 02:33
  • When you say it's working for valid JSON it's behaving how you want it to? – Fintan Creaven Aug 07 '15 at 02:44
  • 1
    @user1655222 One option would be to respond with the name (string) of the function -- `{ "callback": "validate" }`. A simple way to reference a function from that name is to [store it as a method](http://stackoverflow.com/questions/9854995/javascript-dynamically-invoke-object-method-from-string). Example: http://jsfiddle.net/xtz9w6k5/ – Jonathan Lonowski Aug 07 '15 at 02:53
  • @JonathanLonowski your solution is working fine. can you post this as answer. i will mark it as answer. – Vallabha Aug 07 '15 at 18:50

1 Answers1

0

On success, are you sure you mean to call:

success: InitailiseJQB

There may be an accidental spelling error there.

uncleoptimus
  • 360
  • 2
  • 8