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;
},
1 Answer 1
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– Edwin Widhiyanto2017年11月25日 05:58:29 +00:00Commented Nov 25, 2017 at 5:58 -
$.postis for POST. What do you want in this case?Khoa Truong– Khoa Truong2017年11月25日 06:00:20 +00:00Commented Nov 25, 2017 at 6:00 -
When i checked the console, there's error
Cannot read property 'post' of undefinedEdwin Widhiyanto– Edwin Widhiyanto2017年11月27日 03:32:28 +00:00Commented Nov 27, 2017 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 Truong– Khoa Truong2017年11月27日 04:14:29 +00:00Commented Nov 27, 2017 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– Edwin Widhiyanto2017年11月27日 04:33:43 +00:00Commented Nov 27, 2017 at 4:33