📜 ⬆️ ⬇️

Access to SOAP web services 1C from JavaScript and Html

The described method allows you to access 1C web services from an html page via JavaScript. As an example, a list of directories. Clicking on any directory displays the first letters of the items. Clicking on a letter displays data with names beginning with this letter.

The method is applicable for cases when the web service and html-page are published on the same server. In this case, there are no cross-domain problems. For example, if the domains are different, then Chrome will give the error:

Failed to load resource: Origin localhost:3299 is not allowed by Access-Control-Allow-Origin

Without going into details of publishing web services, suppose that, on the side of 1C, the web service catalogs has been created and published with the operation Execute. At the input is the script parameter of the string type, and the output is the string type. The operation launches on the side any script code from the parameter and returns JSON serialization from the result variable.
')
  ExecuteCommands(script) result = null; Execute(script); return JSON(result);  


It is convenient to work with JavaScript tools with JSON serialization and convert a string to an object / array with one eval command (resultText). On the Internet you can find several JSON serializers for 1C.

Make sure the web service responds by typing its address:

Web service response


It is better to start the project of the HTML file with a ready-made template, where styles are written for all browsers. A very decent template is located at html5boilerplate.com The template includes jQuery 1.9.1.

On the form above, we place the web server configuration elements: wsUrl is the web service address, wsUser is the login, wsPassword is the password. On the side of the web service 1C enabled the backgrounddrirization. Login and password correspond to the user registered in 1C.

The left panel is responsible for displaying the available catalogsList directories, the right panel is responsible for displaying letters (letters) and data (catalogRecords).

Javascript



The function to access the SOAP web service is defined as follows:

  function executeSoap(script, successHandler) { var wsUrl = $("#wsUrl").val(); var soapRequest = '<?xml version="1.0" encoding="utf-8"?>' + '<soapenv:Envelope ' + 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' + 'xmlns:api="http://www.1cbit.ru/dominicana/soap-html-js" ' + 'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' + 'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">' + '<soapenv:Body>' + '<api:Execute soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' + '<' + 'script xsi:type="xsd:string">' + script + '</' + 'script>' + '</api:Execute>' + '</soapenv:Body>' + '</soapenv:Envelope>'; $.ajax({ type: "POST", url: wsUrl, contentType: "text/xml", dataType: "xml", username: $("#wsUser").val(), password: $("#wsPassword").val(), data: soapRequest, success: successHandler, error: processError }); } 


At the entrance to the executeSoap, a 1C script is supplied, which is executed on the 1C side, and a handler for successful execution. In case of an error, a processError handler will be called, which will display an error message.

Code 1C is written in html-code via script-tag. You can get the text of each piece of code for execution via jQuery $ ("# enumerate1CCatalogs"). Text ().

Getting a list of directory names.

  <script id="enumerate1CCatalogs" type="text/1c"> result =  ();     .   =  ("name, synonym"); .name = .; .synonym = .; result.(); ; </script> 


Getting the first letters of directory names {catalog}

  <script id="getFirstLetters" type="text/1c">  =  (); . = "  (, 0, 1)  letter  .{catalog}   (, 0, 1)"; result = .().(); </script> 


Getting data for the catalog {catalog}, where the first letter is in the condition {condition}.

  <script id="getCatalogData" type="text/1c">  =  (); . = "  as ref,  as name  .{catalog}  (, 0, 1)  ({condition})"; result = .().(); </script> 


Clicking on the Refresh button calls the function.

executeSoap($("#enumerate1CCatalogs").text(), processSuccess);

and if successful, the processSuccess handler is called

  function processSuccess(data, status, req) { var resultText = $(req.responseText).find("m\\:return").html(); result = eval(resultText); $("#catalogRecords").empty(); catalogsList = $("#catalogsList"); catalogsList.empty(); $(result).each(function (index, item) { var li = '<li catalog="' + item.name + '">' + item.synonym + '</li>'; catalogsList.append( $(li).addClass("catalogTitle") ); }); } 


The web service returns xml, where the contents of the m: return tag - JSON serialization are significant. You can translate it into JavaScript objects via an eval call. The handler clears the list of directories and re-forms it through li-tags with the catalog attribute. Each element is assigned a class catalogTitle.

The web service returns xml, where the contents of the m: return tag - JSON serialization are significant. You can translate it into JavaScript objects via an eval call. The handler clears the list of directories and re-forms it through li-tags with the catalog attribute. Each element is assigned a class catalogTitle.

Similarly handles all controls are handled. Clicking on the directory clears the letters and data, refills the letters. Pressing the letter refills the data from the directory. For processing the code on 1C, pieces of code in script blocks with the type “text / 1c” are responsible.
The application looks like this:

Appearance example

Unsolved authorization issue on IE browser



There is an authorization problem on IE. On IE 8/9, the problem of basic authorization could not be solved by a method similar for other browsers.

Error accessing via Internet Explorer
On IE, Ajax does not work using the user / password properties of $ .ajax. On FF and Chrome everything is working fine. For some reason, the header is not transmitted to the server in the case of IE

Authorization: Basic 0JHQsNGF0YjQuNC10LLQn9CYICjRgNGD0LrQvtCy0L7QtNC40YLQtdC70YwpOg==
If someone knows the reason and how to get around, please write in the comments.

findings



The proposed SOAP-based approach has the right to exist for simple tasks, as it is accompanied by a sufficiently large number of JavaScript code. Perhaps in the future it will be possible to create a JavaScript framework to simplify the process of creating applications.

The developers in this way are independently responsible for security. It is necessary to check the input parameters when writing, not to allow the launch of arbitrary scripts transmitted from the client. The article shows the execution of arbitrary code for example only. You can unify arbitrary query execution, but this is due to the danger of SQL injections.

External components of the Native API from 1C will not work in this environment. This means that you need to further solve the problem with writing drivers for hardware.

Example website: web-site.zip (81,76 kb)

Source: https://habr.com/ru/post/184540/


All Articles