在HP servicemanager(HPSM)中使用jQuery AJAX进行POST

POST using jQuery AJAX in HP servicemanager (HPSM)

本文关键字:jQuery AJAX 进行 POST servicemanager HP HPSM      更新时间:2024-02-19

我知道这是一个很长的机会,但我正在尝试在HPSM中的Javascript工具中使用AJAX进行POST。它的调试能力非常有限,所以我被困在了应该简单的地方(或者我是这么认为的)。从我在其他文章中看到的语法来看,调用AJAX函数应该是正确的,但它似乎不想接受它

感谢的帮助

这是我正在调用的代码,使用jQuery库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");
 }
 });

错误

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

我假设您的HPSM中有一个名为jQuery的ScriptLibrary,对吧?

尝试

lib.jQuery.ajax(...

而不是system.library

不确定是否已将jQuery作为ScriptLibrary导入,但我认为它不会起作用,因为jQuery Library中的代码包含一些对HPSM无效的代码行。

不管怎样。。。

要调用外部RESTful服务,可以使用ScriptLibrary中的doHTTPRequest()函数。

它是什么,需要什么参数等,可以在编程指南中找到:http://86.48.81.222:6080/classic/Content/Resources/PDF/sm_programming_guide.pdf

参见第266页。。。

下面是它应该如何工作的一个简短示例(它从HPSM调用RESTneneneba API来创建一个新事件:

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:目前doHTTPRequest有一个限制。它无法正确处理捕获情况。

因此,即使出现错误,您也会得到字符串形式的响应。而不是对象或任何响应。

INFO2:这个例子是基于我的例子调用内部事件api。我已将其修改为您给定的数据。

使用HPSM 9.3+成功创建并测试了代码。

希望这能有所帮助。

问候Marcus