0

Good Day,

i am new to angularjs and started playing around mostly on the pulling of data trough web service.

my web service returns a list of latitude and longitude from my database

here is the code

[WebMethod]
        public List<friendlyforces> get_friendly(string regionid , string type)
        {
            List<friendlyforces> friendlies = new List<friendlyforces>();
            List<SqlParameter> parameters = new List<SqlParameter>();
            parameters.Add(new SqlParameter("@regid", regionid));
            parameters.Add(new SqlParameter("@type", type));
            SqlDataReader readers = SqlHelper.ExecuteReader(connection, "friendlyforces", parameters.ToArray());
            while (readers.Read())
            {
                friendlies.Add(new friendlyforces());
                friendlies[(friendlies.Count - 1)].unit_name = readers["unit_name"].ToString();
                friendlies[(friendlies.Count - 1)].address = readers["adrress"].ToString();
                friendlies[(friendlies.Count - 1)].latitude = readers["latitude"].ToString();
                friendlies[(friendlies.Count - 1)].longitude = readers["longitude"].ToString();
                friendlies[(friendlies.Count - 1)].icon = readers["icon"].ToString();
            }

            return friendlies;
        }

now on my angularjs enabled page i am trying to display the data given by the web service here is the code

var app = angular.module('Services', []);

        app.controller('latController', function ($scope, $http) {

            var url = "Services/datapull.asmx/get_friendly";

            $http({
                method: 'POST',
                url: url,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                data: JSON.stringify({ regionid: "7", type: "pnp" })
            }).success(function (data) {
                var myjson = JSON.parse(data);

                $scope.locations = JSON.parse(myjson);
            });

        })

now when the web service is invoked it returns an error stating that i am missing a parameter the regionid i tried $.param : ({regionid : "7" , type : "pnp" }) andparam : JSON.stringify ({regionid : "7" , type : "pnp"})

but it retruns the same error parameter is missing

any suggestions?

thank you

1
  • What does the signature on the datapull.asmx method look like?
    – Chris
    Commented May 28, 2015 at 2:11

1 Answer 1

0

From the content type you provide 'Content-Type': 'application/x-www-form-urlencoded', it seems you want to send your data as url param.

However,

By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json".

Try add transformRequest in your request.

$http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
       var str = [];
       for(var p in obj)
       str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
       return str.join("&");
    },
    data: { regionid: "7", type: "pnp" }
    }).success(function () {});
3
  • thank you the web service returned as expected but the format is very new to me <ArrayOfFriendlyforces xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="w3.org/2001/XMLSchema" xmlns="tempuri.org"> <friendlyforces> <unit_name>504th Maritime Police Station</unit_name> <address>Camaligan Fishing Port Complex,Bgy Dugcal, Camaligan, Cam Sur</address> <latitude>123.1570000</latitude> <longitude>13.6263864</longitude> <icon>/icons/PNP.png</icon> </friendlyforces></ArrayOfFriendlyforces> how can i parse this?
    – angelogogo
    Commented May 28, 2015 at 2:13
  • You are returning XML from your controller, which is difficult to handle in Angular. Try return JSON data.
    – Rebornix
    Commented May 28, 2015 at 2:15
  • You can use JavaScriptSerializer to serialize your data to JSON string.
    – Rebornix
    Commented May 28, 2015 at 2:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.