0

My webservice return data, I replace unnecessary character and get valid string.

How that string transfer in object.

Example 1 static data (not from web service) work fine.

$scope.str = [];
str = {"car": [{"id": 11,"name": "BMW",}, {"id": 22,"name": "Toyota"}],};
$scope.data = str;

Example 2 data from web service not working

str = data;
str.replace("?", "").replace("(", "").replace(")", "").replace(";", "");
$scope.data = str;
5
  • 2
    Can you post what the response from your web service looks like ? Commented Nov 15, 2015 at 19:12
  • Please include a sample web service response in your question. Commented Nov 15, 2015 at 19:12
  • 1
    You should be more specific. What doe you mean it is not working? Did you check to make sure data in line 1 of example 2 is not null or undefined? If it is it will definitely cause problems on the next line. Commented Nov 15, 2015 at 19:12
  • Mmmh... if your service return json (string) then you already have an object on client side, no? If it's returning something else, maybe you are creating unnecessary problems for yourself? Commented Nov 15, 2015 at 19:29
  • @groooves here is full source: jsbin.com/nuxanayuca/edit?html,js,output Commented Nov 15, 2015 at 19:53

3 Answers 3

1

After cleaning up your string, you just need to run JSON.parse(),

try this:

str = data;
JSON.parse(str.replace('?','').replace('(','').replace(')','').replace(';','').replace(',}','}').replace(',};','}'))
$scope.data = JSON.parse(str);

REGEX version:

JSON.parse(data.replace(/\?|\(|\)/g, '').replace(/,}/g, '}').replace(/;/g, ''));

Working Copy:

<!DOCTYPE html>
<html ng-app="app">

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular-sanitize.js"></script>
  <meta charset=utf-8 />
  <title></title>
</head>

<body ng-controller="MyController">
  <option ng-repeat="cars in data.car" value="{{cars.name}}">{{cars.name}}</option>

  <button ng-click="calculateQuantity()">Calculate</button>
  <script>
    var app = angular.module('app', ['ngSanitize']);

    app.controller("MyController", function($scope, $http) {


      $scope.calculateQuantity = function() {

        $http.get('https://crossorigin.me/http://wsvuci.int-ware.com/appWS.asmx/myService?callback=?', {
            params: {
              userName: 'aa',
              procedureName: "EXECUTE wsReturn"
            }
          })
          .success(function(data) {
            $scope.data = JSON.parse(data.replace(/\?|\(|\)/g, '').replace(/,}/g, '}').replace(/;/g, ''));
          })
          .error(function() {
            alert("error");
          });
      };

    });
  </script>

</body>

</html>

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

1 Comment

Not working here is full source jsbin.com/nuxanayuca/edit?html,js,output
0

replace() returns a string. It doesn't store the result in the "str" variable. Try this:

str = data;
str = str.replace("?", "").replace("(", "").replace(")", "").replace(";", "");
$scope.data = str;

Comments

0

This is JSONP format! You don't need to cleanup anything. Just define a valid callback name and use $http.jsonp (). In the documentation you'll find also a working sample.

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.