1

I could call web service from block with code below : $jsonObject=json_decode(file_get_contents("http://www.testservice.com:50001/Services/Magento.asmx/getPayer?input=".$datasJson));

How can i call service from java script? I tried with code below, but it's all not work.

urlBuilder.createUrl('http://wsq.starcosmosgroup.com:50001/Services/Magento.asmx/getPayer?input=', {}),
JSON.stringify({
     parameter1: 'data1',
     parameter2: 'data2'
}

UPDATE :

I tried to get the data from service for change the billing address data in checkout page. I adding the code below, and add 'jquery' to the define, but it made the other function error, so how can i get the data from the service?

billing-address.js :

getBill: function (prefix) {
   payload = {
        parameter1: prefix,
        parameter2: 'data'
   };
   url = "http://www.testservice.com:50001/Services/Magento.asmx/getPayer?input="+JSON.stringify(payload);

   alert(url);
   $.get(url, function(response) {
        data = response;
        alert(response);
   });
   return data;
},
0

I suggest for you two ways:

1) Magento Ajax > Magento controller > Call the external server

In this case, we need to build the Magento controller. We can use a buit-in class Magento\Framework\HTTP\Client\Curl to work with HTTP protocol.

2) Magento Ajax > Call the external server directly.

We will have an issue with CORS (Cross-Origin Resource Sharing). So, in this case, make sure the external server allows accessing from your Magento server.


We simply use $.ajax, $.get, $.post for Ajax call.

  • i tried payload = { parameter1: prefix, parameter2: 'test' }; $.post("http://www.testservice.com:50001/Services/Magento.asmx/getPayer?input=".JSON.stringify(payload), function(response) { data = response; }); return data; but it's not work, am i doing something wrong in my code? – Edwin Widhiyanto Nov 25 '17 at 5:58
  • $.post is for POST. What do you want in this case? – Khoa TruongDinh Nov 25 '17 at 6:00
  • When i checked the console, there's error Cannot read property 'post' of undefined – Edwin Widhiyanto Nov 27 '17 at 3:32
  • Did you add jquery to require? One more, do you want to post data to the server? Don't want to get the data? – Khoa TruongDinh Nov 27 '17 at 4:14
  • No, i dont add any require thing in the js, and i want to post data then get the response data, should i use get not post? – Edwin Widhiyanto Nov 27 '17 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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