2

When I use jquery $ajax, I get the data as json But when using angular http service, I get the response as xml.

This is my both code (angular and jquery ajax)

var _getList = function () {
    var list = [];
    var deferred = $q.defer();
    $.ajax({
        type: "POST",
        url: '/Landing/manage/WebService.asmx/GetList',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data && data.d) {
                list = data.d;
                deferred.resolve(list);
            }
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            deferred.reject(xmlHttpRequest);
        }
    });      

   //angular
    $http({
        method: 'POST',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: '/Landing/manage/WebService.asmx/GetList',          
        headers: {
            "Content-Type": "application/json"
        }
    }).success(function (data) {
        console.log(data);
          deferred.resolve(data);
      })
     .error(function (data, status, headers, config) {
         deferred.reject(data);
     });

    return deferred.promise;
};

And this is my web method code With json format return

 [WebMethod]
    [System.Web.Script.Services.ScriptMethod(ResponseFormat = 

    System.Web.Script.Services.ResponseFormat.Json)]
        public tblCampaignCollection GetList()
        {
            tblCampaignCollection coll =  Campaign.AdminGetAll();

            return coll;
    }

enter image description here

1 Answer 1

3

The request you are performing is not a real POST Request, I had a similar issue, if you read the console network TAB you will see that it is a GET request.

What I do if I want to perform a POST in a service is the following:

function myService(xxxParam) {

      var request = $http({
        method: 'POST',
        headers: {"Content-Type": 'text/plain; charset=UTF-8'},
        url: serviceURL,
        data: {
          firstPostParam: "string",
          secondPostParam: 1,
          thirdPostParam: xxxParam
        }
      });
      return ( request.then(handleSuccess, handleError) );
    }

try with

$http({
      method: 'POST',
      url: '/Landing/manage/WebService.asmx/GetList',
      headers: {
        "Content-Type": 'application/json; charset=UTF-8'
      },
      data: {
        dataType:  "json"
      }
    })...

I hope it helps.

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

1 Comment

adding data: { dataType: "json" } resolve my problem

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.