1

I have an existing WCF (non-RESTful) service that I'm calling using $.ajax. I need to be able to use $http service. I've tried out a few things, but nothing seems be working. The below snippet returns xml successfully, and I'm ok with it as I can't change the service to return json.

var Type = "POST";
var Url = "http://localhost:83928/BookReviewService.svc";
var Data = '<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GetBookReviews xmlns="http://tempuri.org/"><bookReviewsRequest xmlns:a="http://schemas.datacontract.org/2004/07/BookModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:AmazonCustomerReviewUrl i:nil="true"/><a:AmazonSiteLinkUrl i:nil="true"/><a:Isbn>0393324826</a:Isbn></bookReviewsRequest></GetBookReviews></s:Body></s:Envelope>';
var ContentType = "text/xml; charset=utf-8";
var DataType = "xml";
var ProcessData = true;
CallService();

function CallService() {
    $.ajax({
        type: Type, //GET or POST or PUT or DELETE verb
        url: Url, // Location of the service
        data: Data, //Data sent to server
        contentType: ContentType, // content type sent to server
        dataType: DataType, //Expected data format from server
        processdata: ProcessData, //True or False
        beforeSend: function (xhr) {
            xhr.setRequestHeader("SOAPAction", "http://tempuri.org/IBookReviewService/GetBookReviews");
        },
        success: function (msg) {//On Successfull service call
            ServiceSucceeded(msg);
        },
        error: ServiceFailed// When Service call fails
    });
}

function ServiceFailed(result) {
    console.log(result);
    console.log('Service call failed: ' + result.status + ' ' + result.statusText);
}

function ServiceSucceeded(result) {
    console.log(result);
}
1
  • I'd like to abandon this question as I found a completely different way to get this done. Commented Aug 26, 2015 at 14:52

1 Answer 1

1

Your question reads "convert ajax call to angularjs http post".

To start with you should create a service/factory for all your ajax operations, but that's just a personal preference, would work without that too, read this if you don't want to use service/factory and do directly instead.

angular.module("moduleName").factory('factoryName', ['$http', function ($http) {
    return {
        myFunction: function(data) {
            return $http({
                url: '/user/update',
                method: 'POST',
                data: data
            });
        }
    };
}]);

and in your controller, inject this service and use this function like

factoryName.myFunction();

Ofcourse with a success/error callbacks if needed.

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

1 Comment

Yasser this is a general format of using the $http service. I need to know how I need to convert the soap data I'm passing to JSON. Also, I've added the SOAPAction header, I like to know if this is required.

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.