Can't connect MM2 with external service

Can't connect MM2 with external service

by Zoran Jeremic -
Number of replies: 5
My service returns an array that looks like this:

$response=array();
$response[]=array("id"=>1,"name"=>"dashboard");

The service returns are described as follows:

return new external_multiple_structure(
new external_single_structure(
array(
'id' => new external_value(PARAM_INT, 'component id'),
'name' => new external_value(PARAM_TEXT, 'component name, unique')
)
));

On the MM2 side, I tried to access the service as:

    $mmSite.read('local_morph_get_morph_components', data, presets).then(function(response){
console.log("SERVICE CALL RESPONSE:"+JSON.stringify(response));
});
However, whatever I do, I could not make this work, even though very similar service that has only one bool variable instead of int and text which I have here, works fine. The error I got is:
error

I think this service description is fine, but I don't know how to investigate what is the problem.

Zoran

Average of ratings: -
In reply to Zoran Jeremic

Re: Can't connect MM2 with external service

by Frédéric Massart ⭐ -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi,

Are data and presets objects?

Cheers,

Fred

In reply to Frédéric Massart ⭐

Re: Can't connect MM2 with external service

by Zoran Jeremic -

Data and presets are initialized as:


var data = {
       courseid : courseId
       },
presets = {};
$mmSite.read('local_morph_get_morph_components', data, presets).then(function(response){
console.log("SERVICE CALL RESPONSE:"+JSON.stringify(response));
});
and I think everything is fine with it, since service is called on PHP side, and it responds to the request, so it returns array("id"=>1,"name"=>"dashboard");. However, this response is not received on MM2 side, and I don't know how to investigate where the problem is.
I have another service that receives the same input, but instead of INT and TEXT variables in array it returns one BOOL variable, so response is array("activated"=>true). The other service works fine.


In reply to Zoran Jeremic

Re: Can't connect MM2 with external service

by Frédéric Massart ⭐ -
Picture of Core developers Picture of Plugin developers Picture of Testers

I cannot think of any reason why this happens... could you try to locate the line:


return $http.post(siteurl, ajaxData).then(function(data) {


And add a console.log(data); right after it? This is located in the $mmWS factory.

In reply to Frédéric Massart ⭐

Re: Can't connect MM2 with external service

by Zoran Jeremic -

Hi Frederic,


As you suggested I found this function and added following lines:


        var ajaxData = data;
        console.log("siteurl:"+siteurl);
        console.log("ajaxData:"+JSON.stringify(ajaxData));
        return $http.post(siteurl, ajaxData).then(function(data) {
            console.log("DATA:"+JSON.stringify(data));


Console output looks like:

Error output

Service is executed on the Moodle side, but the body of function(data) is not executed.





In reply to Zoran Jeremic

Re: Can't connect MM2 with external service

by Zoran Jeremic -

After several days of trying to figure out where the problem is, I managed to solve this. In one of the methods used within service implementation I had echo which outputs some values which I used while I was testing the service. I forgot to delete it, and it has been returned as a part of the service response, which prevented service client to read returned value properly. Unfortunately, there was no indication that something is wrong with service itself, so I didn't pay too much attention on this. 


Zoran