52787

convert ajax call to angularjs http post

Question:

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); }

Answer1:

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 <a href="https://docs.angularjs.org/api/ng/service/$http" rel="nofollow">this</a> 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.

Recommend

  • Show generated SQL in toplink in eclipse
  • Select an attribute with Xpath in a XML with namespace using Powershell
  • Spring custom user details service null pointer exception
  • ExtensionDataObject not marked as serializable
  • SessionInformation expiration doesn't lead to user log out from system
  • How can I compose a WCF contract out of multiple interfaces?
  • Streamed transfer restrictions with WCF
  • Storing WCF rest request data with SQL Server stored procedure
  • Feature detection of foreignObject in SVG
  • XSD with multi occurrences unordered
  • Jetty 9 HashLoginService
  • Read a local file using javascript
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • does jqgrid support a multiple checkbox list for editing
  • Optimizing database types to compact database (SQLite)
  • Cross-Platform Protobuf Serialization
  • Do I've to free mysql result after storing it?
  • Apache 2.4 - remove | delete | uninstall
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Free memory of cv::Mat loaded using FileStorage API
  • Trying to get generic when generic is not available
  • Getting Messege Twice Using IMvxMessenger
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • How to get Windows thread pool to call class member function?
  • Bitwise OR returns boolean when one of operands is nil
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?