2

I am using http post to get the data from a webservice using the following code..

$http({
                method: "post",
                url: "SurveyQuestions.asmx/GetSurveyQuestions",
                data: JSON.stringify({"empl_id" : '12345' , "Year" : '2015' }),
                headers: { 'Content-Type': 'application/json' },
                dataType: "json",
                contentType: "application/json; charset=utf-8",
            }).success(function (response) { //do something})

The above code is working fine using "get" without parameters.

and my webservice for the post is

<WebMethod()> _
        <ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)> _
        Public Sub GetSurveyQuestions(ByVal Empl_id as string, ByVal Year as string)
        Try
        //get list of records
          Dim js As JavaScriptSerializer = New JavaScriptSerializer

          Context.Response.Write(js.Serialize(listSurveyQuestions))
        Catch ex As Exception

      End Try

    End Sub

Web service is executing fine and returning the data to the Method, but the response is empty in the $http and errored out.

Charlie
  • 22,886
  • 11
  • 59
  • 90
vamsivanka
  • 792
  • 7
  • 16
  • 36
  • 1
    What is the error? – Charlie Jul 15 '16 at 05:20
  • @CharlieH Unexpected token { in JSON at position 3483 at Object.parse (native). FYI, when i remove the parameters and change to "get" it works. same code change to "post" throws the error. – vamsivanka Jul 15 '16 at 13:47
  • @CharlieH It is working now. I didn't know, that i can use "get" and include parameters in the URL itself. Thanks. – vamsivanka Jul 15 '16 at 14:46

2 Answers2

0

you just try to pass the object only adding at the last in url.

This my case i am just adding the employee Id. if you send only employee object only it will also correct.

$scope.removeEmployee = function(employee) {

var method = 'DELETE';

var url = 'http://localhost:8080/AngularJSWithRestful/rest/product/Delete/' + employee.id  ;

$http({

    method : method,
    url : url,

}).then(function successCallback(response) {

    $scope.product = response.data.product;

    }, function errorCallback(response) {

    });
};
Charlie
  • 22,886
  • 11
  • 59
  • 90
seri
  • 1
0

Because you are retrieving data and there are no changes being executed on your server (according to your comment) the best thing to do would be to change it to a HttpGet method. Then in angular you should use params property in the passed object to the $http function. This is because HttpGet does not support a message body. From the documentation:

params{Object.<string|Object>} – Map of strings or objects which will be serialized with the paramSerializer and appended as GET parameters.

Your changed code:

$http({
    method: "GET",
    url: "SurveyQuestions.asmx/GetSurveyQuestions",
    params: {"empl_id" : '12345' , "Year" : '2015' }, // use the params property
    headers: { 'Content-Type': 'application/json' },
    dataType: "json",
    contentType: "application/json; charset=utf-8",
}).then(function (response) { //do something})

Also note that success and error are being marked as obsolete by the angular team. The recommended approach is to use then instead. See the same documentation link.

Deprecation Notice

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

See also AngularJS passing data to $http.get request

Community
  • 1
  • 1
Igor
  • 60,821
  • 10
  • 100
  • 175