问题
I want to use CL_JAVA_SCRIPT in abap to execute a javascript file : jQuery.js which i get from url. And this is my code :
REPORT ZCLIENT.
DATA: LV_CLIENT TYPE REF TO IF_HTTP_CLIENT.
DATA:lv_URL TYPE STRING .
lv_URL = 'https://resources/jquery.js'.
* Create client
CALL METHOD CL_HTTP_CLIENT=>CREATE_BY_URL
EXPORTING
URL = lv_URL
IMPORTING
CLIENT = LV_CLIENT
EXCEPTIONS
ARGUMENT_NOT_FOUND = 1
PLUGIN_NOT_ACTIVE = 2
INTERNAL_ERROR = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* LV_CLIENT->REQUEST->SET_CONTENT_TYPE(
* EXPORTING
* CONTENT_TYPE = 'application/javascript; charset=UTF-8'
* ).
CALL METHOD LV_CLIENT->REQUEST->SET_FORMFIELD_ENCODING
EXPORTING
FORMFIELD_ENCODING = LV_CLIENT->REQUEST->CO_ENCODING_URL.
* Get request:
CALL METHOD LV_CLIENT->SEND
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
OTHERS = 4.
IF SY-SUBRC NE 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
* Prepare client-receive:
CALL METHOD LV_CLIENT->RECEIVE
EXCEPTIONS
HTTP_COMMUNICATION_FAILURE = 1
HTTP_INVALID_STATE = 2
HTTP_PROCESSING_FAILED = 3
OTHERS = 4.
* Get HTML:
DATA EV_DATA TYPE STRING.
CONSTANTS co_line_size TYPE i VALUE 150.
TYPES: t_line TYPE c LENGTH co_line_size.
DATA: l_string TYPE string,
lt_lines TYPE TABLE OF t_line.
FIELD-SYMBOLS: <l_line> TYPE t_line.
EV_DATA = LV_CLIENT->RESPONSE->GET_CDATA( ).
" compile js
data GET_VALUE type STRING.
data JS_PROCESSOR type ref to CL_JAVA_SCRIPT.
data SOURCE type STRING.
concatenate EV_DATA SOURCE into SOURCE SEPARATED BY cl_abap_char_utilities=>cr_lf.
JS_PROCESSOR = CL_JAVA_SCRIPT=>CREATE( ).
JS_PROCESSOR->COMPILE( SCRIPT_NAME = 'TEST_GET.JS'
SCRIPT = EV_DATA ).
if JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
write: / 'Error in COMPILE',
JS_PROCESSOR->LAST_ERROR_MESSAGE.
else.
write / 'JavaScript was compiled'.
endif.
skip.
JS_PROCESSOR->EXECUTE(
exporting SCRIPT_NAME = 'TEST_GET.JS' ).
if JS_PROCESSOR->LAST_CONDITION_CODE <> 0.
write: / 'Error in EXECUTE',
JS_PROCESSOR->LAST_ERROR_MESSAGE.
else.
write / 'JavaScript was executed'.
endif.
skip.
my file is well compiled but When executing my code i'm getting some errors like "window is not defined" .
How should resolve the problem
回答1:
jQuery is a JavaScript library designed to work with the browser's document object model (DOM) and it used for the UI, animations, client side logic, etc. It is built on top of the DOM and all functionality depends on that, trying to execute the jQuery.js file on an ABAP system will not work as there is no browser and there is no DOM for jQuery to interact with.
You can find further information on the ABAP server and what it does and does not do at the SAP website ABAP Application Server page.
来源:https://stackoverflow.com/questions/35148676/execute-jquery-file-from-abap-using-cl-java-script