使用SOAP.jquery访问Dynamics NAV Web服务时遇到问题(跨域问题)

时间:2018-05-29 14:51:44

标签: jquery apache soap cors navision

我正在尝试从外部服务器连接到Nav Dynamics Web Services与运行它的位置相比。

我的导航服务器位于nav-dev.mydomain.local,我正在尝试从neotime.mydomain.com连接到它。

我尝试访问Nav的主机的Apache配置如下:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log "combined"

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf

    Header always set Access-Control-Allow-Headers "Authorization"
    Header always set Access-Control-Allow-Methods "GET, OPTIONS"
    Header always set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Expose-Headers "WWW-Authenticate"
    LogLevel debug
</VirtualHost>

我已经安装了@ Doedje的soap.jquery插件(https://github.com/doedje/jquery.soap),我的登录网站服务如下:

&#13;
&#13;
$(document).ready(function() {
	$('.login_form').submit(function(event) {
		event.preventDefault();

		var login = $('#username').val();
		var password = $('#password').val();
		ws_login = '*web_service_login';
		ws_password = '*web_service_password';
		console.log('Form login: ' + login);
		console.log('Form password: ' + password);



		$.soap({
			enableLogging: true,
			url: 'http://nav-dev.creatives.local:7247/NAV_DEV_WS/WS/CREATIVES%20SANDBOX/Codeunit/WS_GESTION/',
			namespaceQualifier: 'ws',
			method: 'CtrlLoginCreatives',
			envAttributes: {
				'xmlns:ws': 'urn:microsoft-dynamics-schemas/codeunit/WS_GESTION'
			},
			HTTPHeaders: {
				'Access-Control-Allow-Origin': '*',
				'Authorization': 'Basic ' + btoa(ws_login + ':' + ws_password)
			},
			data: {
				'pCompany': 'CREATIVES%20SANDBOX',
				'pLogin': login,
				'pPass': password
			},
			beforeSend: function(SOAPEnvelope) {
				// SOAPEnvelope.setRequestHeader('Authorization', 'Basic ' + btoa(ws_login + ':' + ws_password));
				console.log(SOAPEnvelope);
			},
			success: function(soapResponse) {
				console.log('Success with soapResponse');
				// do stuff with soapResponse
				// if you want to have the response as JSON use soapResponse.toJSON();
				// or soapResponse.toString() to get XML string
				// or soapResponse.toXML() to get XML DOM
			},
			error: function(SOAPResponse) {
				// show error
				console.log('Error with SOAPResponse');
			}
		});
	});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- <!DOCTYPE html> -->
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
		<title>Creatives Timesheet</title>
		<meta name="description" content="">
		<meta name="keyword" content="">
		<!-- Compressed CSS -->
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/foundation-sites@6.4.3/dist/css/foundation.min.css" integrity="sha256-GSio8qamaXapM8Fq9JYdGNTvk/dgs+cMLgPeevOYEx0= sha384-wAweiGTn38CY2DSwAaEffed6iMeflc0FMiuptanbN4J+ib+342gKGpvYRWubPd/+ sha512-QHEb6jOC8SaGTmYmGU19u2FhIfeG+t/hSacIWPpDzOp5yygnthL3JwnilM7LM1dOAbJv62R+/FICfsrKUqv4Gg=="
			crossorigin="anonymous">
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css" integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp" crossorigin="anonymous">
		<link href="https://fonts.googleapis.com/css?family=Montserrat:300,600" rel="stylesheet">
		<link rel="stylesheet/less" href="includes/css/style.less" type="text/css" media="screen" title="no title" charset="utf-8">
		<!-- Compressed JavaScript -->
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://cdn.jsdelivr.net/npm/foundation-sites@6.4.3/dist/js/foundation.min.js" integrity="sha256-mRYlCu5EG+ouD07WxLF8v4ZAZYCA6WrmdIXyn1Bv9Vk= sha384-KzKofw4qqetd3kvuQ5AdapWPqV1ZI+CnfyfEwZQgPk8poOLWaabfgJOfmW7uI+AV sha512-0gHfaMkY+Do568TgjJC2iMAV0dQlY4NqbeZ4pr9lVUTXQzKu8qceyd6wg/3Uql9qA2+3X5NHv3IMb05wb387rA=="
			crossorigin="anonymous"></script>
		<script src="https://cdnjs.cloudflare.com/ajax/libs/less.js/3.0.4/less.min.js"></script>
		<script src="http://momentjs.com/downloads/moment-with-locales.min.js" charset="utf-8"></script>
		<script src="http://underscorejs.org/underscore-min.js" charset="utf-8"></script>
		<script src="includes/js/clndr.min.js"></script>
	</head>
	<body>
		<header class="expanded row ">
			<div class="columns small-8 medium-2 large-1 small-centered">
				<img src="img/logo.svg" alt="">
			</div>
		</header>
		<main class="row login">
			<div class="small-10 medium-5 large-4 small-centered columns">
				<form class="login_form row" method="post">
					<input class="small-12 columns" type="text" id="username" name="Username" placeholder="Username" value="">
					<input class="small-12 columns" type="password" id="password" name="password" value="" placeholder="Password">
					<button id="login_button" class="button small-12 columns" type="submit" value="Submit">Login</button>
				</form>
			</div>
		</main>
		<script src="includes/js/vendor/what-input.js"></script>
		<script src="includes/js/app.js"></script>
		<script src="includes/js/he.js"></script>
		<script src="includes/js/soap.js"></script>
		<script src="includes/js/ntlm.js"></script>
		<script src="includes/js/web_services.js"></script>
		<script src="includes/js/site.js"></script>
	</body>
</html>
&#13;
&#13;
&#13;

我对CORS问题深感不安,因为Nav服务器一直在使用错误401进行响应。我没有解决方法可以解决它。

以下是检查员日志

jquery.min.js:2 OPTIONS http://nav-dev.creatives.local:7247/NAV_DEV_WS/WS/CREATIVES%20SANDBOX/Codeunit/WS_GESTION/CtrlLoginCreatives 401 (Unauthorized)
send @ jquery.min.js:2
ajax @ jquery.min.js:2
send @ soap.js:281
soap @ soap.js:129
(anonymous) @ web_services.js:15
dispatch @ jquery.min.js:2
y.handle @ jquery.min.js:2
  

login.html:1无法加载   http://nav-dev.creatives.local:7247/NAV_DEV_WS/WS/CREATIVES%20SANDBOX/Codeunit/WS_GESTION/CtrlLoginCreatives:对预检请求的响应未通过访问控制检查:否   &#39;访问控制允许来源&#39;标题出现在请求的上   资源。起源&#39; http://neotime.creatives.com&#39;因此不是   允许访问。响应的HTTP状态代码为401。

和HTTP标头

Request:
OPTIONS /NAV_DEV_WS/WS/CREATIVES%20SANDBOX/Codeunit/WS_GESTION/CtrlLoginCreatives HTTP/1.1
Host: nav-dev.creatives.local:7247
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://neotime.creatives.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36
Access-Control-Request-Headers: access-control-allow-origin,authorization,content-type,soapaction
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7
X-CookiesOK: I explicitly accept all cookies


Response:
HTTP/1.1 401 Unauthorized
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
WWW-Authenticate: Basic realm=""
Date: Tue, 29 May 2018 14:56:54 GMT

非常感谢你的帮助!

^ Q

0 个答案:

没有答案