3

In my application, Asp web service returns JSON with object name called 'd' so I access that 'd' object the in the application as follows,

enter image description here

GetBranchOrRegionDataSourceSuccess: function (result, status, init) {
    "use strict";
    var regions = JSON.parse(result.d);
}

I called this function inside the Ajax success call.

Now the problem is I have Jquery function called searchLocations, Inside that function, I need to call this function. and need to pass the parameters.

I tried like this,

var jsonResult = JSON.stringify({'d':result});
  this.GetBranchOrRegionDataSourceSuccess(jsonResult,"Success", true); //here I need to call the function

this is my complete function.

function searchName(prov,tree) {
  var result = [];

  let searchKey = new RegExp(prov, "i");

  var objects = JSON.parse(json);
  for (obj of objects) {
    if (obj.Name.match(searchKey)) {

      result.push(obj); 

    } else {
      var toAdd = {"Id": obj.Id, "Name": obj.Name, "Branches": []};
      for (branch of obj.Branches) {
        if (branch.Name.match(searchKey)) {
          toAdd.Branches.push(branch);
        }
      }
      if (toAdd.Branches.length) {
        result.push(toAdd);
      }
    }
  }

  var jsonResult = JSON.stringify({'d':result});
  this.GetBranchOrRegionDataSourceSuccess(jsonResult,"Success", true); //here I need to call the function

}

But it makes error inside GetBranchOrRegionDataSourceSuccess on this line var regions = JSON.parse(result.d); How can I pass the result with 'd' object name

4
  • FYI RegExp.prototype.test() is faster than String.prototype.match() if you're just checking Commented May 6, 2019 at 4:34
  • @Phil You mean I should need to replace all the match functions with test() ? Commented May 6, 2019 at 4:39
  • Only if you want to and it would be like if(searchKey.test(branch.Name)) Commented May 6, 2019 at 4:40
  • @Phil Okay sir Thanks Commented May 6, 2019 at 4:44

1 Answer 1

2

GetBranchOrRegionDataSourceSuccess is expecting an object with property d.

The d property value needs to be a JSON string (which it will parse).

You want

let jsonResult = { d: JSON.stringify(result) }
this.GetBranchOrRegionDataSourceSuccess(jsonResult, 'Success', true)
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.