Teach Me NAV
A site to support our book 'Implementing Microsoft Dynamics NAV 2009'.

Web Service Call From HTML

rated by 0 users
Answered (Verified) This post has 1 verified answer | 6 Replies | 3 Followers

Top 10 Contributor
Male
3 Posts
Alexander Viedge posted on 10-12-2009 9:45 p.m.

Hi, 

it tried your example to call a web service from an HTML page. But the response text contains a message saying that the "SOAP Message is invalid". Do you have any idea what's wrong?

Best regards

Alex

Answered (Verified) Verified Answer

Top 10 Contributor
Male
389 Posts

Well I finally got time to try this out today and I have a solution.

I used Fiddler2 on the 2009 Sp1 VPC image to examine the SOAP envelope when the Web service is called from Visual Studio and compared this to the call made from within the HTML page.

The "real" content of the SOAP envelope was identical except my call included some Tabs and Spaces (things that should be ignored in the XML anyway). For some reason the SP1 version rejects this as an invalid SOAP message. Weird!

To get this working, change the setting of the SOAP envelope in the HTML page to:

var data = "";
data += '<?xml version="1.0" encoding="utf-8"?>';
data += '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ';
data += 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
data += 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
data += '<soap:Body>';
data += '<ConvertStrToUpperCase ';
data += 'xmlns="urn:microsoft-dynamics-schemas/codeunit/NAV_Codeunit">';
data += '<p_Str>hello nav2009!</p_Str>';
data += '</ConvertStrToUpperCase>';
data += '</soap:Body>';
data += '</soap:Envelope>';

It's not as easy to read, but it works!

Eventually I'm going to write a little blog post about this and update the downloads so that there is a 2009 and 2009 SP1 version. In the meantime, this should give you enough information to get going again.

Dave.

All Replies

Top 10 Contributor
Male
389 Posts

Hi Alex,

which example are you doing? Did you try to key in the code or use the sample downloads. If you keyed it in, maybe you could send me your HTML file, if not let me know which file you are using.

Which version of NAV are you using NAV 2009 or NAV 2009 SP1. Are you using Internet Explorer to run the Web Page?

I'll send you an e-mail so you can reply with any files or attachments.

It's been a while since I did this example and I haven't tried it since SP1 was released.

Cheers,

Dave.

Top 25 Contributor
1 Posts

Hi,

I bought youre ebook implementing NAV.

In chapter 7, you write some examples to access NAV through Web services.

I found the page HelloNAV2009.htm, change the URL of the web service (mine is http://localhost:7047/DynamicsNAV/ws/CRONUS%20France%20S.A./Codeunit/NAV_Codeunit).

When i click the button "Call NAV", it returns me an error with the line containing "resultText = xmlDoc.getElementsByTagName("return_value")[0].childNodes[0].nodeValue;".

The error when executing this HTM page through Visual Studio 2008 is "Microsoft JScript Error : object is required".

did you have any idea of that ?

Thanks a lot.

Best regards,

Olivier.

Top 10 Contributor
Male
3 Posts

Hi Olivier,

the error occurs because the "return_value" doesn't exist. Obviously Microsoft changed something inside the Web Service, so this example can't work properly (under SP1). We don't know exactly what they changed but I'm trying to discover (just as David does, I think...:-))

Best regards,

Alex

Top 10 Contributor
Male
389 Posts

Well I finally got time to try this out today and I have a solution.

I used Fiddler2 on the 2009 Sp1 VPC image to examine the SOAP envelope when the Web service is called from Visual Studio and compared this to the call made from within the HTML page.

The "real" content of the SOAP envelope was identical except my call included some Tabs and Spaces (things that should be ignored in the XML anyway). For some reason the SP1 version rejects this as an invalid SOAP message. Weird!

To get this working, change the setting of the SOAP envelope in the HTML page to:

var data = "";
data += '<?xml version="1.0" encoding="utf-8"?>';
data += '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ';
data += 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ';
data += 'xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
data += '<soap:Body>';
data += '<ConvertStrToUpperCase ';
data += 'xmlns="urn:microsoft-dynamics-schemas/codeunit/NAV_Codeunit">';
data += '<p_Str>hello nav2009!</p_Str>';
data += '</ConvertStrToUpperCase>';
data += '</soap:Body>';
data += '</soap:Envelope>';

It's not as easy to read, but it works!

Eventually I'm going to write a little blog post about this and update the downloads so that there is a 2009 and 2009 SP1 version. In the meantime, this should give you enough information to get going again.

Dave.

Top 10 Contributor
Male
3 Posts

Hi Dave,

I have another question concerning the Web service. I'm trying to call the Web service from an Insert trigger inside the SQL server. Unfortunately the Web service says "Microsoft.Dynamics.Nav.Types.NavDatabasePasswordException" and "The user ID and password are invalid. Try again."

Do you have any idea how I could set the credentials to get it working?

The SQL code of the trigger is attached below this message.

Kind regards,

Alex

USE [Demo Database NAV (6-0)]
GO

/****** Object:  Trigger [dbo].[InsertTrigger]    Script Date: 11/04/2009 11:07:13 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER [dbo].[InsertTrigger]
   ON  [dbo].[Testtabelle]
   AFTER INSERT
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
    -- Insert statements for trigger here

DECLARE @hr int
DECLARE @chrXML varchar (4000)
DECLARE @chrLength nvarchar (8)
SET @chrXML = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConvertStrToUpperCase
xmlns="urn:microsoft-dynamics-schemas/codeunit/Test">
<p_Str>hello nav2009!</p_Str>
</ConvertStrToUpperCase>
</soap:Body>
</soap:Envelope>'

SET @chrLength = LEN(@chrXML)

DECLARE @chrStatus nvarchar (4000)
SET @chrStatus = ''

DECLARE @token int, @hdoc int, @xml varchar(8000)
EXEC sp_OACreate 'WinHttp.WinHttpRequest.5.1', @token OUT
EXEC sp_OAMethod @token, 'open', NULL, 'GET','http://localhost:7047/DynamicsNAV/WS/CRONUS AG/Codeunit/Test', false
EXEC sp_OAMethod @token, 'setRequestHeader', NULL, 'Content-Type', 'text/xml'

EXEC xp_logevent 60000, @chrXML
EXEC @hr = sp_OAMethod @token, 'send', NULL, @chrXML

IF @hr < 0 BEGIN
  PRINT @hr
  EXEC @hr = sp_OAGetErrorInfo @token
END
 
exec @hr = sp_OAGetProperty @token, 'status', @chrStatus OUT
if @chrStatus <> 200 begin print @chrStatus END

EXEC xp_logevent 60000, @chrStatus

EXEC sp_OAMethod @token, 'responseText', @xml OUT

EXEC xp_logevent 60000, @xml

EXEC sp_OADestroy @token
 
SELECT @xml

END

GO

Top 10 Contributor
Male
389 Posts

Hi Alex,

I'm afraid I can't help you with this because it is not something I have ever done before. Maybe you should post the question on mibuso or dynanmicsuser.

As you probably know the browser example works because Internet Explorer handles the negotiation to use your credentials to call the web service. The way you are doing this in SQL Server is obviously not doing this.

I would question whether you should be trying to call a web service in an insert trigger on a SQL table. Beaer in mind that when you insert a record you typically want it to be fast and not to fail for some dependency on another system. You would be breaking both of these "rules" with what you are trying to do. If you are trying to do interfacing, maybe you should write values away to another table in the SQL trigger which will be fast and then have a scheduled job or other task written in .NET that processes the records and calls the Web service.  I don't know what your DBAs are like but I can't imagine any I work with being happy to stick this kind of code into a production database.

Good luck with your task.

Cheers,

Dave.

Page 1 of 1 (7 items) | RSS
Powered by Community Server (Non-Commercial Edition), by Telligent Systems