cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
1973
Views
0
Helpful
4
Replies

Error REST API with JavaScript in Cisco Network Service Orchestrator (NSO)

AmonSilva1454
Level 1
Level 1

Hi, dears.

 

I'm new with NSO, and I'm having difficult to find resources about JavaScript with NSO. I'm trying to connect the NSO with a webpage that I did, but it's returning this message:

 

 

Access to XMLHttpRequest at 'http://127.0.0.1:8080/api/running/' from origin 'http://127.0.0.1:34201' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 

 

 

 

I followed the Web UI Documentation, I used the Postman code generated, but it continue to return this same CORN error message.

 

What make me more confuse is that the connection return success with Postman, but isn't with my webpage.

 

Postman jQuery code Ajax:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "http://127.0.0.1:8080/api/running/",
  "method": "GET",
  "headers": {
    "Authorization": "Basic YWRtaW46YWRtaW4=",
    "cache-control": "no-cache",
    "Postman-Token": "19627c94-1ea8-4a14-9688-c08c7a8d99e5"
  }
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

 

Postman Headers return:

Server →
Date →Fri, 01 Mar 2019 18:04:50 GMT
Last-Modified →Fri, 01 Mar 2019 17:50:31 GMT
Cache-Control →private, no-cache, must-revalidate, proxy-revalidate
Etag →1551-414835-261704
Content-Type →application/vnd.yang.datastore+xml
Transfer-Encoding →chunked
Pragma →no-cache

 

Postman body return:

<data xmlns:y="http://tail-f.com/ns/rest">
    <example xmlns="http://example.com/add-devices">
        <y:operations>
            <add-devices-and-sync>/api/running/example/_operations/add-devices-and-sync</add-devices-and-sync>
        </y:operations>
        <nacm xmlns="urn:ietf:params:xml:ns:yang:ietf-netconf-acm">
            <write-default>permit</write-default>
            <groups>
                <group>
                    <name>admin</name>
                </group>
                <group>
                    <name>oper</name>
                </group>
            </groups>
            <rule-list>
                <name>admin</name>
            </rule-list>
            <rule-list>
                <name>any-group</name>
            </rule-list>
        </nacm>
        <maapi-types xmlns="http://examples.com/maapi">
            <mtypes>
                <ilist>123</ilist>
                <ilist>456</ilist>
                <ilist>789</ilist>
                <slist>The Grey Fox</slist>
                <slist>The White Horse</slist>
                <firstname>George</firstname>
                <a_number>42</a_number>
                <b_number>7</b_number>
                <indexes>
                    <index>
                        <x>1</x>
                    </index>
                    <index>
                        <x>2</x>
                    </index>
                    <index>
                        <x>8</x>
                    </index>
                </indexes>
                <movables>
                    <movable>
                        <a>1</a>
                        <b>2</b>
                    </movable>
                </movables>
                <servers>
                    <server>
                        <srv-name>smtp</srv-name>
                    </server>
                    <server>
                        <srv-name>www</srv-name>
                    </server>
                    <server>
                        <srv-name>x100</srv-name>
                    </server>
                </servers>
                <dks>
                    <dk>
                        <dk-name>zapp</dk-name>
                        <ip>66.77.7.7</ip>
                    </dk>
                </dks>
                <types>
                    <c_int8>77</c_int8>
                    <c_int16>77</c_int16>
                    <c_int32>77</c_int32>
                    <c_int64>77</c_int64>
                    <c_uint8>77</c_uint8>
                    <c_uint16>77</c_uint16>
                    <c_uint32>77</c_uint32>
                    <c_uint64>77</c_uint64>
                    <b>true</b>
                    <f>4.660000000000000</f>
                    <c_ipv4>1.2.3.4</c_ipv4>
                    <c_ipv6>fe80::</c_ipv6>
                    <datetime>2001-10-16T21:32:52+02:00</datetime>
                    <date>2001-10-26+02:00</date>
                    <duration>P1Y</duration>
                    <enum>15-mins</enum>
                    <objectref xmlns:mt="http://examples.com/maapi">/mt:maapi-types/mt:mtypes/mt:servers/mt:server[mt:srv-name='smtp']/mt:ip</objectref>
                    <bits>turboMode</bits>
                    <ipv4Prefix>192.168.0.0/16</ipv4Prefix>
                    <ipv6Prefix>2001:db8::1428:57a8/125</ipv6Prefix>
                </types>
                <ints>
                    <int>
                        <i>3</i>
                    </int>
                    <int>
                        <i>5</i>
                    </int>
                </ints>
            </mtypes>
        </maapi-types>
        <snmp xmlns="http://tailf.com/ns/snmp">
            <agent>
                <enabled>true</enabled>
                <ip>0.0.0.0</ip>
                <udp-port>4000</udp-port>
                <version>
                    <v1/>
                    <v2c/>
                    <v3/>
                </version>
                <engine-id/>
                <max-message-size>50000</max-message-size>
            </agent>
            <system>
                <contact></contact>
                <name></name>
                <location></location>
            </system>
            <usm>
                <local>
                    <user>
                        <name>initial</name>
                    </user>
                </local>
            </usm>
            <target>
                <name>monitor</name>
            </target>
            <community>
                <index>public</index>
            </community>
            <notify>
                <name>foo</name>
            </notify>
            <vacm>
                <group>
                    <name>initial</name>
                </group>
                <group>
                    <name>public</name>
                </group>
                <view>
                    <name>internet</name>
                </view>
                <view>
                    <name>restricted</name>
                </view>
            </vacm>
        </snmp>
        <aaa xmlns="http://tail-f.com/ns/aaa/1.1">
            <authentication>
                <users>
                    <user>
                        <name>admin</name>
                    </user>
                    <user>
                        <name>oper</name>
                    </user>
                    <user>
                        <name>private</name>
                    </user>
                    <user>
                        <name>public</name>
                    </user>
                </users>
            </authentication>
        </aaa>
        <ssh xmlns="http://tail-f.com/ns/ncs">
            <host-key-verification>none</host-key-verification>
        </ssh>
        <cluster xmlns="http://tail-f.com/ns/ncs">
            <authgroup>
                <name>default</name>
            </authgroup>
        </cluster>
        <devices xmlns="http://tail-f.com/ns/ncs">
            <global-settings>
                <trace-dir>./logs</trace-dir>
            </global-settings>
            <authgroups>
                <group>
                    <name>default</name>
                </group>
                <snmp-group>
                    <name>default</name>
                </snmp-group>
            </authgroups>
            <mib-group>
                <name>snmp</name>
            </mib-group>
            <device>
                <name>ex0</name>
            </device>
            <device>
                <name>ex1</name>
            </device>
            <device>
                <name>ex2</name>
            </device>
            <y:operations>
                <connect>/api/running/devices/_operations/connect</connect>
                <sync-to>/api/running/devices/_operations/sync-to</sync-to>
                <sync-from>/api/running/devices/_operations/sync-from</sync-from>
                <disconnect>/api/running/devices/_operations/disconnect</disconnect>
                <check-sync>/api/running/devices/_operations/check-sync</check-sync>
                <check-yang-modules>/api/running/devices/_operations/check-yang-modules</check-yang-modules>
                <fetch-ssh-host-keys>/api/running/devices/_operations/fetch-ssh-host-keys</fetch-ssh-host-keys>
                <clear-trace>/api/running/devices/_operations/clear-trace</clear-trace>
                <partial-sync-from>/api/running/devices/_operations/partial-sync-from</partial-sync-from>
            </y:operations>
        </devices>
        <java-vm xmlns="http://tail-f.com/ns/ncs">
            <stdout-capture>
                <file>./logs/ncs-java-vm.log</file>
            </stdout-capture>
            <jmx/>
            <y:operations>
                <stop>/api/running/java-vm/_operations/stop</stop>
                <start>/api/running/java-vm/_operations/start</start>
                <restart>/api/running/java-vm/_operations/restart</restart>
            </y:operations>
        </java-vm>
        <smart-license xmlns="http://tail-f.com/ns/ncs">
            <smart-agent>
                <stdout-capture>
                    <file>./logs/ncs-smart-licensing.log</file>
                </stdout-capture>
                <y:operations>
                    <restart>/api/running/smart-license/smart-agent/_operations/restart</restart>
                </y:operations>
            </smart-agent>
            <y:operations>
                <register>/api/running/smart-license/_operations/register</register>
                <deregister>/api/running/smart-license/_operations/deregister</deregister>
                <renew>/api/running/smart-license/_operations/renew</renew>
                <show>/api/running/smart-license/_operations/show</show>
            </y:operations>
        </smart-license>
        <python-vm xmlns="http://tail-f.com/ns/ncs">
            <logging>
                <log-file-prefix>./logs/ncs-python-vm</log-file-prefix>
            </logging>
            <y:operations>
                <stop>/api/running/python-vm/_operations/stop</stop>
                <start>/api/running/python-vm/_operations/start</start>
            </y:operations>
        </python-vm>
        <services xmlns="http://tail-f.com/ns/ncs">
            <global-settings>
                <collect-forward-diff>true</collect-forward-diff>
            </global-settings>
            <logging>
                <logger>
                    <name>default</name>
                </logger>
            </logging>
            <y:operations>
                <check-sync>/api/running/services/_operations/check-sync</check-sync>
                <commit-dry-run>/api/running/services/_operations/commit-dry-run</commit-dry-run>
            </y:operations>
        </services>
        <software xmlns="http://tail-f.com/ns/ncs">
            <packages>
                <y:operations>
                    <list>/api/running/software/packages/_operations/list</list>
                    <upload>/api/running/software/packages/_operations/upload</upload>
                    <fetch>/api/running/software/packages/_operations/fetch</fetch>
                    <install>/api/running/software/packages/_operations/install</install>
                    <deinstall>/api/running/software/packages/_operations/deinstall</deinstall>
                </y:operations>
            </packages>
        </software>
        <alarms xmlns="http://tail-f.com/ns/ncs-alarms">
            <alarm-model>
                <alarm-type>
                    <type>connection-failure</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>revision-error</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>out-of-sync</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>dev-manager-internal-error</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>ned-live-tree-connection-failure</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>configuration-error</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>bad-user-input</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>commit-through-queue-failed</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
                <alarm-type>
                    <type>commit-through-queue-blocked</type>
                    <specific-problem-match>*</specific-problem-match>
                </alarm-type>
            </alarm-model>
            <y:operations>
                <synchronize-alarms>/api/running/alarms/_operations/synchronize-alarms</synchronize-alarms>
                <compress-alarms>/api/running/alarms/_operations/compress-alarms</compress-alarms>
                <filter-alarms>/api/running/alarms/_operations/filter-alarms</filter-alarms>
                <purge-alarms>/api/running/alarms/_operations/purge-alarms</purge-alarms>
            </y:operations>
        </alarms>
        <scheduler xmlns="http://tail-f.com/ns/ncs-scheduler">
            <y:operations>
                <suspend>/api/running/scheduler/_operations/suspend</suspend>
                <resume>/api/running/scheduler/_operations/resume</resume>
            </y:operations>
        </scheduler>
        <operations>
            <lock>/api/running/_lock</lock>
            <rollback>/api/running/_rollback</rollback>
        </operations>
    </data>

My jQuery code (the access-control headers is commented because it was return a error in the Header Authentication, but I included for more information):

var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://127.0.0.1:8080/api/running/",
    "type":'xml',
    "method": "GET",
    "headers": {
        "Authorization": "Basic YWRtaW46YWRtaW4=",
        "cache-control": "no-cache",
        "Postman-Token": "a5fc5a92-8d6b-49d8-a68d-62f9b7503af4",
//        "Access-Control-Allow-Origin": "*",
//        "Access-Control-Allow-Credentials": "true",
//        "Access-Control-Allow-Headers": "Origin, Content-Type, Accept",
//        "Access-Control-Request-Method": "GET"
    },
    dataType: 'xml',
    xhrFields: {
        withCredentials: true
    }

}

$.ajax(settings).done(function (response) {
    console.log(response);
});

Thanks for your help guys.

4 Replies 4

richsun
Cisco Employee
Cisco Employee

Try the following config in ncs.conf

 <rest>
    <enabled>true</enabled>
    <showHidden>false</showHidden>
    <customHeaders>
          <header>
          <name>Access-Control-Allow-Origin</name>
          <value>*</value>
          </header>
	  <header>
          <name>Access-Control-Allow-Credentials</name>
          <value>true</value>
          </header>
          <header>
          <name>Access-Control-Allow-Methods</name>
          <value>GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS</value>
          </header>
          <header>
          <name>Access-Control-Allow-Headers</name>
          <value>Accept, Accept-CH, Accept-Charset, Accept-Datetime, Accept-Encoding, Accept-Ext, Accept-Features, Accept-Language, Accept-Params, Accept-Ranges, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Request-Headers, Access-Control-Request-Method, Age, Allow, Alternates, Authentication-Info, Authorization, C-Ext, C-Man, C-Opt, C-PEP, C-PEP-Info, CONNECT, Cache-Control, Compliance, Connection, Content-Base, Content-Disposition, Content-Encoding, Content-ID, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Script-Type, Content-Security-Policy, Content-Style-Type, Content-Transfer-Encoding, Content-Type, Content-Version, Cookie, Cost, DAV, DELETE, DNT, DPR, Date, Default-Style, Delta-Base, Depth, Derived-From, Destination, Differential-ID, Digest, ETag, Expect, Expires, Ext, From, GET, GetProfile, HEAD, HTTP-date, Host, IM, If, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Keep-Alive, Label, Last-Event-ID, Last-Modified, Link, Location, Lock-Token, MIME-Version, Man, Max-Forwards, Media-Range, Message-ID, Meter, Negotiate, Non-Compliance, OPTION, OPTIONS, OWS, Opt, Optional, Ordering-Type, Origin, Overwrite, P3P, PEP, PICS-Label, POST, PUT, Pep-Info, Permanent, Position, Pragma, ProfileObject, Protocol, Protocol-Query, Protocol-Request, Proxy-Authenticate, Proxy-Authentication-Info, Proxy-Authorization, Proxy-Features, Proxy-Instruction, Public, RWS, Range, Referer, Refresh, Resolution-Hint, Resolver-Location, Retry-After, Safe, Sec-Websocket-Extensions, Sec-Websocket-Key, Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Version, Security-Scheme, Server, Set-Cookie, Set-Cookie2, SetProfile, SoapAction, Status, Status-URI, Strict-Transport-Security, SubOK, Subst, Surrogate-Capability, Surrogate-Control, TCN, TE, TRACE, Timeout, Title, Trailer, Transfer-Encoding, UA-Color, UA-Media, UA-Pixels, UA-Resolution, UA-Windowpixels, URI, Upgrade, User-Agent, Variant-Vary, Vary, Version, Via, Viewport-Width, WWW-Authenticate, Want-Digest, Warning, Width, X-Content-Duration, X-Content-Security-Policy, X-Content-Type-Options, X-CustomHeader, X-DNSPrefetch-Control, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto, X-Frame-Options, X-Modified, X-OTHER, X-PING, X-PINGOTHER, X-Powered-By, X-Requested-With </value>
          </header>
    </customHeaders>
  </rest>

richsun
Cisco Employee
Cisco Employee

Try this config in ncs.conf

 <rest>
    <enabled>true</enabled>
    <showHidden>false</showHidden>
    <customHeaders>
          <header>
          <name>Access-Control-Allow-Origin</name>
          <value>*</value>
          </header>
	  <header>
          <name>Access-Control-Allow-Credentials</name>
          <value>true</value>
          </header>
          <header>
          <name>Access-Control-Allow-Methods</name>
          <value>GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS</value>
          </header>
          <header>
          <name>Access-Control-Allow-Headers</name>
          <value>Accept, Accept-CH, Accept-Charset, Accept-Datetime, Accept-Encoding, Accept-Ext, Accept-Features, Accept-Language, Accept-Params, Accept-Ranges, Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Access-Control-Allow-Origin, Access-Control-Expose-Headers, Access-Control-Max-Age, Access-Control-Request-Headers, Access-Control-Request-Method, Age, Allow, Alternates, Authentication-Info, Authorization, C-Ext, C-Man, C-Opt, C-PEP, C-PEP-Info, CONNECT, Cache-Control, Compliance, Connection, Content-Base, Content-Disposition, Content-Encoding, Content-ID, Content-Language, Content-Length, Content-Location, Content-MD5, Content-Range, Content-Script-Type, Content-Security-Policy, Content-Style-Type, Content-Transfer-Encoding, Content-Type, Content-Version, Cookie, Cost, DAV, DELETE, DNT, DPR, Date, Default-Style, Delta-Base, Depth, Derived-From, Destination, Differential-ID, Digest, ETag, Expect, Expires, Ext, From, GET, GetProfile, HEAD, HTTP-date, Host, IM, If, If-Match, If-Modified-Since, If-None-Match, If-Range, If-Unmodified-Since, Keep-Alive, Label, Last-Event-ID, Last-Modified, Link, Location, Lock-Token, MIME-Version, Man, Max-Forwards, Media-Range, Message-ID, Meter, Negotiate, Non-Compliance, OPTION, OPTIONS, OWS, Opt, Optional, Ordering-Type, Origin, Overwrite, P3P, PEP, PICS-Label, POST, PUT, Pep-Info, Permanent, Position, Pragma, ProfileObject, Protocol, Protocol-Query, Protocol-Request, Proxy-Authenticate, Proxy-Authentication-Info, Proxy-Authorization, Proxy-Features, Proxy-Instruction, Public, RWS, Range, Referer, Refresh, Resolution-Hint, Resolver-Location, Retry-After, Safe, Sec-Websocket-Extensions, Sec-Websocket-Key, Sec-Websocket-Origin, Sec-Websocket-Protocol, Sec-Websocket-Version, Security-Scheme, Server, Set-Cookie, Set-Cookie2, SetProfile, SoapAction, Status, Status-URI, Strict-Transport-Security, SubOK, Subst, Surrogate-Capability, Surrogate-Control, TCN, TE, TRACE, Timeout, Title, Trailer, Transfer-Encoding, UA-Color, UA-Media, UA-Pixels, UA-Resolution, UA-Windowpixels, URI, Upgrade, User-Agent, Variant-Vary, Vary, Version, Via, Viewport-Width, WWW-Authenticate, Want-Digest, Warning, Width, X-Content-Duration, X-Content-Security-Policy, X-Content-Type-Options, X-CustomHeader, X-DNSPrefetch-Control, X-Forwarded-For, X-Forwarded-Port, X-Forwarded-Proto, X-Frame-Options, X-Modified, X-OTHER, X-PING, X-PINGOTHER, X-Powered-By, X-Requested-With </value>
          </header>
    </customHeaders>
  </rest>

Hi, Richsun.

 

I altered the ncs.config in my NCS-run folder and in my NSO-4.7/etc/ncs (installation folder)

 

When I altered it just in my NCS-run folder, and after I restart the setup in this folder with the ncs-setup --reset, and tried to run it, the terminal returned me the message:

 

showHidden cannot occur here as a child to rest.

Removing this tag, it returned me this message:

customHeaders cannot occur here as a child to rest.

When I altered it in my NSO-4.7 folder, I used the ncs-setup --reset command in my NCS-run folder and I started the NSO in it also. It ran normally, but continue to return the same error of authenticate for web page.

 

Thank you for your reply.

The sample config is copied from my confd.conf, which should work for ncs.conf. Try the following changes:

1. Remove the whole line for tag showHidden

2. Change tag <customHeaders> to <custom-headers>

I just tested it on NSO 4.7 ncs.conf, NSO starts OK. Probably not all headers are needed to solve your case, give it a try.

 

Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the NSO Developer community: