My Software Notes

Useful things I discover

Archive for the ‘JavaScript’ Category

Solving Time for Browsers and Servers

leave a comment »

The person using the browser is in time zone A and the server is in time zone B. How do we display the time and how do we save the time so we don’t end up with a Doctor Who style time distortion?

Here is a quick list of the methods I’ve come across.

(NOTE: always save dates and times to your database in UTC, never in local time)

  1. Pass the time-zone offset on each call:
    1. Put it in a cookie using JavaScript then read the cookie on the server

    2. Put it in a header

    3. Put it in a query string parameter

  2. Allow user to set the time zone as a preference and save it in the database
  3. Use moment.js or a similar library to convert the time to UTC each time before you pass it back
  4. Put the time-zone offset into a hidden field in a form so it is part of the POST back to the server

I prefer the cookie and the user preference methods.  I also use the moment.js method where it is applicable.

References – these contain details of how to do the things mentioned above:

How to get the time zone offset in the browser:

var d = new Date()
var timezoneOffset = d.getTimezoneOffset();

 

Written by gsdwriter

July 1, 2020 at 10:53 am

CRM 2011 – Ajax call using jQuery returns “No Transport” error

with 2 comments

I was trying to call the OData service in CRM.  I was using the good old jQuery $.getJSON function.  It was failing with the “errorThrown”  parameter telling me “No Transport”.

I had a vague recollection of hitting this error in a different context last year, so I looked it up and found that the error can be caused by a cross-domain Ajax request.

For Example:

Your browser url is “http://crm/MyCorp” and your Ajax call uses http://server03/.

Gotcha

On the surface, this didn’t really explain my problem.  My browser was pointing at my CRM installation and I was making a call to the OData service on the same installation.  So why the cross domain issue?

Then it hit me.  There were two ways to get to the CRM installation.  There was a DNS entry for “crm” and there was one for the server itself called “crmserver03”.  I checked what url was being used by running the JavaScript debugger (good old F12) and there was the answer:  My browser was pointing at http://crm/MyCorp and the Ajax call was going to http://crmserver03/MyCorp.  Huh?

I hunted a bit more and here is what I found:  I was using the Xrm.Page.context.getServerUrl function to make sure I had the right server url BUT this function does not return the domain the browser is using.  Instead it returns the domain that was set in the config of the CRM installation.

See:  How does “Xrm.Page.context.getServerUrl” work?

So if you navigated to the CRM installation using the domain name “crm” and the config says “crmserver03”, then calling getServerUrl will return a different domain to the one your browser is using.  In this case, if you use getServerUrl to build the URL for an Ajax call then you will be using a different domain name and will get the “No Transport” error (unless you’ve done this: How to make a cross-domain Ajax call in jQuery).

I think that’s a bug in gerServerUrl.

Anyway.  Here is my fix:

var url = window.location.protocol +  "//" +  
    window.location.host + "/" + 
    Xrm.Page.context.getOrgUniqueName();

Or, for a relative url, you can use:

var url = "/" + Xrm.Page.context.getOrgUniqueName();

Whichever suits your needs.

Update: See comment below from Carsten Groth and my reply for a new Xrm function that returns the correct url.

Written by gsdwriter

January 7, 2013 at 1:48 pm

MVC 4 Beta – Single Page Application Tutorial – Gotcha

with 6 comments

I’m currently trying out the ASP.NET MVC 4 Beta and I thought I’d give the Single Page Application tutorial a shot.

I’ve created several single page apps so far and they’re working well and people are using them successfully, but now that I’ve had some experience I look at them and wish I could rewrite them from scratch.  If you look back at code you wrote six months ago and don’t think that you could do it better now then either you are perfect or you have stopped learning and are fast becoming antiquated.

Anyway, I want to start using frameworks like knockout.js and backbone.js, and the SPA (Single Page Application) template that comes with MVC 4 uses knockout.js – great.

So I followed the instructions, hit F5 to run the web site, typed “Tasks” in the address bar and … aaargh!  An exceptions in the view and a really unhelpful error message.  “System.InvalidOperationException was unhandled by user code” and “Failed to get the MetadataWorkspace for the DbContext type ‘MvcSpaApp1.Models.MvcSpaApp1Context’.”  The inner exception was no better: “System.Data.ProviderIncompatibleException” and “The provider did not return a ProviderManifestToken string.”

Luckily I’ve been reading up on Entity Framework Code First, so I knew what the problem was – I didn’t have SQL Server Express running.  So I started up SQL Server Express and … still didn’t work.  But this time it was because I didn’t have rights to SQL Server because I was using a machine someone else set up and they had mucked up the security.  Anyway, when I finally got it all sorted out the site ran without any problems.

So just be aware: YOU MUST HAVE SQL SERVER EXPRESS RUNNING and YOU MUST HAVE DATABASE AND TABLE CREATION RIGHTS.

Other than those gotchas the tutorial is good.

Written by gsdwriter

March 2, 2012 at 11:29 am

Installing npm (Node Package Manager) on Windows

with 4 comments

A while ago I tried and tried and tried and tore out my hair trying to install npm on Windows (in case you’re new to node-js, npm is the Node Package Manager – sort of a NuGet for Node).

I followed the instructions but I got all sorts of annoying errors that I just could not get around no matter what I tried.  So I gave up.

A couple of days ago I saw that there was a nice looking book called “Node for Front-End Developers” that looked like it didn’t need any packages installed, so I bought it and decided to get the latest version of Node.  I went to the Node website and there on the home page was a download button that popped up a choice of Node versions for Windows, Mac or any other platform (by getting the source and building it yourself).

I ran the Windows installer and (hoo-friggin-ray!) it installed npm at the same time.

The new installer is nice and simple – it just runs and installs Node.  What more can you ask for in an installer?  (Well, maybe giving the choice of where to install Node would be good, in case you don’t want to put it in “c:\program files”.)

One gotcha about running npm in Windows 7 – if you are installing something globally, you must run npm as an administrator because it is under “c:\program files” and any changes you want to make in that area require admin privileges.

Anyway, I got started on the book, which is very hands-on, and I’m learning more and more about Node.

I’ll blog more about it as I discover the wonders of JavaScript on the server.

Written by gsdwriter

February 7, 2012 at 2:33 pm

Posted in JavaScript, node.js

Tagged with ,

Podcast Introductions to node.js

leave a comment »

I’ve recently come across some good intro podcasts on node.js.

I’ve been listening to the .NET Rocks podcast for a while now and their latest show is a talk with Tomasz Janczuk, the guy at Microsoft who is working on porting node.js to Windows and IIS. It’s a good intro for all types of developers but is probably aimed at .NET devs more than open source or Java devs.

» .NET Rocks – Show 711: Tomasz Janczuk Builds Web Apps with node.js – As well as the basics on what node.js is and how you use it, there is some very good data on when you’d use iisnode versus basic node.exe.

I only came across the Herding Code podcast a few weeks ago, but I like it so much that I’ve already caught up on all the shows for this year and a couple of the earlier ones that covered topics I’m interested in. The four presenters are real pro devs and the discussions are always really lively, funny and interesting.

There are two node.js related shows:

» Herding Code 102: Tim Caswell on Node.js – this is an excellent intro that goes beyond the basics and explains the event loop of node and the non-blocking async programming model it uses. This gives you a good foundation.

» Herding Code 122: Bert Belder on porting Node.js to Windows – This is about more than just porting node to Windows, which is very interesting in itself, it builds on show 102, so listen to that one first.

I hope these are helpful.

Written by gsdwriter

November 2, 2011 at 10:14 am

Installing nodejs and iisnode on Windows

with 7 comments

The instructions I used are at WebMatrix and node.js: The easiest way to get started with node on Windows

Here’s how it went for me:

  1. WebMatrix Install:  It took a looooong time to install, about 30 minutes.  I was running quite a few things on my machine at the time, including VS2010 and SQL Mgmt Studio, but it still seemed like a heckuva long time, so be patient.
  2. node.js for Windows Install:  If you save to the default downloads folder in IE9 you’ll get the “This program is not commonly downloaded and could harm your computer” message.  Click on “Actions” > “More actions” and “Run anyway”. Or you can open the folder in Windows explorer and double-click the file.  Once you get that far the rest is easy.  It installs to “C:\Program Files\nodejs”  Add it to your path so you can play with the REPL and can run .js scripts.  Once it’s in your path, just type “node” at the command prompt and there you have a JavaScript REPL!  Or type “node myfile.js” and run JavaScript directly on Windows.  Nice!
  3. iisnode for iis7 express (x86) Install: If you don’t have Microsoft Visual C++ 2010 Redistributable Package (x86) installed then the iisnode installer will tell you that you need it. Just click my link here and get it. It installs easily and then the iisnode install is a piece of cake. (FYI: You’ll have the same “This program is not commonly … etc.” message if you try to run after downloading in IE9 and that’ll happen on the next two also.)
  4. iisnode for iis7 (x86) Install: For my messing about I didn’t really need to install it but I wanted to anyway. There were no issues.
  5. iisnode for iis7 (x64) Install: I didn’t install this, ’cause I’m using a 32 bit machine for this messing about, but if you are installing the x64 version then I’m guessing you’ll need the C++ 2010 redistributable for x64.
  6. node.js templates for WebMatrix: Easy install.  Thanks to Steve Sanderson (the genius behind knockout.js) for the templates

So that’s all it took.  After that I fired up WebMatrix and created a “Hello World” type app, by using the “Node.js Express Site” template.  Then I plowed around in the packages that come with it: express and jade, etc.  It’s just amazing what you can get open source these days.  There is a fabulous community out there in JavaScriptland.

Finally I wrote a quick testme.js file and ran it from the command line.  You can use straight JavaScript and all the “console” object methods you get in a browser.  When you want to test out node’s built-in objects and cool async methods, you’ll need to check out the node.js documentation and maybe the node.js wiki.

If I get some time free and I feel courageous, I might try to figure out how to use VS2010 to write node apps.  If anyone out there has already done it then please leave a comment – no point in re-inventing the wheel.

Happy hunting!

Written by gsdwriter

September 29, 2011 at 8:36 am

Nodejs for Windows and IIS

with 2 comments

I’ve been programming a lot of JavaScript recently. I’m really enjoying it as a language. It’s spoiling me for the static languages like C#. (But only a little, after all C# 4 has plenty of dynamic features. In fact, if you use the ExpandoObject you can – to some degree – almost write C# like JavaScript).

Anyway, in expanding my JavaScript horizons I’ve started to dabble with node.js, a server-side implementation of JavaScript.  I’ve been using it mainly for the REPL so I can try out stuff to make sure I’m doing it right.

I installed node.js a month or so ago and it wasn’t too much trouble, but I had to install Cygwin and other stuff with it for it to work on Windows.  I wasn’t interested in taking the time to compile it myself, so I used what the node.js guys were good enough to supply.

I wasn’t looking forward to upgrading though, but Tomasz Janczuk came to my rescue!  He’s been working on making node.js work on not just Windows but also in IIS AND IIS Express.  All the links you need to download node.js are here: Current installation packages for node.js and iisnode for Windows.

Instructions on how to get started using it are here:

The ability to use JavaScript on both the client and the server has tremendous potential for making our lives simpler.  Imagine programming both areas using a single language.  It seems like it’s a ways in the future, but who knows in this industry?

Written by gsdwriter

September 27, 2011 at 2:32 pm