POST using jQuery AJAX in HP servicemanager (HPSM)

1.9k views Asked by At

I know this is a long shot, but I'm trying to make a POST with AJAX within the Javascript tool in HPSM. It's got very limited debugging capabilities so I'm stuck where it should be simple (or so I thought). From the syntax I've seen in other articles, calling that AJAX function should be right, but it doesn't seem to want to take it.

Thanks for any help

Here is the code I'm calling, and using jQuery library v1.11.2

var JSONdata = {
"eId": "xxx111",
"deviceToken": "111111111111",
"deviceType": "iphone",
"applicationName": "huds"
 };


 system.library.jQuery.ajax({
 type: "POST",
 url: 'http://place:11400/location/collaboration/notifications/register/',
 data: JSONdata,
 dataType: "json",
 cache: false,
 crossDomain: true,
 processData: true,
 success: function (data) {
     alert(JSON.stringify(data));
 },
 error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert("error");
 }
 });

errors

Process panel calc.javascript in RAD format.cjavascript encountered error in line 5 (format.cjavascript,calc.javascript)
Cannot evaluate expression (format.cjavascript,calc.javascript)
Cannot evaluate expression (format.cjavascript,calc.javascript)
Cannot evaluate expression (format.cjavascript,calc.javascript)
Cannot evaluate expression (format.cjavascript,calc.javascript)
Script <UNKNOWN>  line 20: ERROR TypeError: system.library.jQuery.ajax is not a function at char 1
Script 'jQuery'  line 925: ERROR TypeError: document has no properties at char 1
Unrecoverable error in application:  se.call.process on panel call.rad.1
Unrecoverable error in application:  cm.update.save on panel call.master.upd
Unrecoverable error in application:  format.cjavascript on panel calc.javascript
2

There are 2 answers

0
Ernesto On

I'm assuming you have a ScriptLibrary called jQuery in your HPSM, right?

Try with

lib.jQuery.ajax(...

instead of system.library, regards.

0
noxify On

not sure if you have imported the jQuery as a ScriptLibrary, but I think it will not work, because the code inside the jQuery Library includes some lines of code which are not valid for the HPSM.

Anyway...

To call an external external RESTful Service, you can use the doHTTPRequest() function in your ScriptLibrary.

What it is, what parameters are needed etc. can be found in the Programming Guide: http://86.48.81.222:6080/classic/Content/Resources/PDF/sm_programming_guide.pdf

See Page 266 ...

Here an short example how it should work (it calls the REST API from the HPSM to create a new incident:

var objConfig = {
    "url"       : "http://place:11400",
    "path"      : "/location/collaboration/notifications/register/",
    "connect_timeout"   : 30,
    "read_timeout"      : 30
}; 

var objPostData = {  
    "eId": "xxx111",
    "deviceToken": "111111111111",
    "deviceType": "iphone",
    "applicationName": "huds"
};

createRecord( objPostData );

/**
 * Create a new Incident with the RESTful API
 *
 * @param   {Object} objRecord      Object with all required fields
 *
 */
function createRecord( objRecord ) {

    var JSON = system.library.JSON.json();


    var arrHeaders      = [];

    //Content Type application/json is required
    //otherwise we will get an 501 error
    var typeHeader      = new Header();
    typeHeader.name     = "content-type";
    typeHeader.value    = "application/json";  

    var arrHeaders      = new Array();
    arrHeaders.push(typeHeader);

    //build url for the request
    //Default Action for POST is "create" so we don't need
    //to add the action again
    var cRequestUrl     = objConfig.url+objConfig.path;

    //convert the given object to an json string
    cPostBody           = system.library.JSON2.toJSON(objRecord);

    try {
        //lets run the the HTTP request
        //HTTP Command - url to execute - http header - POST Body
        var rcRequest   = doHTTPRequest( "POST", cRequestUrl, arrHeaders, cPostBody, objConfig.connect_timeout, objConfig.read_timeout );

        //convert response json string back to an object
        var objResponse = JSON.parse(rcRequest);

        print( objResponse.toSource() );

    } catch( e ) {
        //something goes wrong
        //check also http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html
        //to get the description of the given http response code like 401 etc.

        //currently it's not possible (or i don't know how) to get the correct
        //error response - for me it looks like that the HPSM has an filter
        //which removes the response body if the response header is not 200

        //if it's possible to use the reponse, we can use the same code
        //as above JSON.parse() etc.
        print("ERROR: \n"+e);
    }
}

INFO1: Currently there is a limitation in the doHTTPRequest. It can't handle the catch case correctly.

So even when there is an error, you will get the response as string. And not the Object or whatever the response is.

INFO2: This example is based on my example to call the internal incident api. I have modified it to your given data.

Code was created and tested successfully with HPSM 9.3+.

Hope this helps.

Greets Marcus