Mootools & JQuery Ajax calls to an ASP.NET MVC Application

It’s been a while since I’ve messed around with some good ol’ Mootools and JQuery. I hope they’re still in fashion!

This project demonstrate something very simple and something that probably every web application out there has to do nowadays… make ajax calls from JavaScript to a backend  service. Both Mootools and jQuery provide wrappers around that functionality but it can be annoying to get started with them especially when you want to POST different kinds of data – simple strings and numbers, complex objects and lists etc.

The code is on GitHub (you can also download a .zip file from this link)

https://github.com/floatingfrisbee/MooToolsAndJqueryServiceExample

Not much more to say but if you check out the project (an ASP.NET MVC 3 application build using Visual Studio 2010) it is a minimalistic demonstration of what headers you need to set, what additional encoding (using json2) you need and a couple of other nuances.

Here are Mootools calls

// Call a service that takes a simple string as input
function simpleMoo() {
    var args = new Object();
    args.stuff = 'A Compact JavaScript Framework';

    var req = new Request.JSON({
        url: '/home/savestuff',
        method: 'POST',
        onSuccess: OnSuccessMoo,
        onFailure: OnFailureMoo,
        data: args
    });

    req.send();
}

// Call a service that takes an object with a sub object as an argument
function complexMoo() {
    var args = new Object();
    args.Name = 'Vince';
    args.Age = 34;
    args.Address = new Object();
    args.Address.Street = '32 E 23RD ST APT F';
    args.Address.City = 'New York';
    args.Address.State = 'NY';
    args.Address.Country = 'USA';

    var req = new Request.JSON({
        url: '/home/savecomplexstuff',
        method: 'POST',
        onSuccess: OnSuccessMoo,
        onFailure: OnFailureMoo,
        urlEncoded: false
    });

    req.setHeader('Content-Type', 'application/json; charset=utf-8');

    req.send(JSON.encode(args));
}

// Call a service that takes a list as an argument
function listMoo() {
    var args = new Object();

    args.listOfStuff = [];
    args.listOfStuff[0] = "Christoph Pojer";
    args.listOfStuff[1] = "David Walsh";
    args.listOfStuff[2] = "Darren Waddell";

    var req = new Request.JSON({
        url: '/home/savealistofstuff',
        method: 'POST',
        onSuccess: OnSuccessMoo,
        onFailure: OnFailureMoo,
        urlEncoded: false
    });

    req.setHeader('Content-Type', 'application/json; charset=utf-8');

    req.send(JSON.encode(args));
}

And here are the JQuery calls

// Call a service that takes a simple string as input
function simpleJQuery() {
    var args = new Object();
    args.stuff = 'Write Less Do More';

    $.ajax({
        url: '/home/savestuff',
        type: 'POST',
        data: args,
        success: OnSuccessJQuery,
        error: OnFailureJQuery
    });
}

// Call a service that takes an object with a sub object as an argument
function complexJQuery() {
    var args = new Object();
    args.Name = 'Vince';
    args.Age = 23;
    args.Address = new Object();
    args.Address.Street = '45 W 43TH ST APT E';
    args.Address.City = 'New York';
    args.Address.State = 'NY';
    args.Address.Country = 'USA';

    $.ajax({
        url: '/home/savecomplexstuff',
        type: 'POST',
        data: JSONDC.stringify(args),
        contentType: 'application/json; charset=utf-8',
        success: OnSuccessJQuery,
        error: OnFailureJQuery
    });
}

// Call a service that takes a list as an argument
function listJQuery() {
    var args = new Object();

    args.listOfStuff = [];
    args.listOfStuff[0] = "John Resig";
    args.listOfStuff[1] = "Leah Silber";
    args.listOfStuff[2] = "Adam Sontag";

    $.ajax({
        url: '/home/savealistofstuff',
        type: 'POST',
        data: JSONDC.stringify(args),
        contentType: 'application/json; charset=utf-8',
        success: OnSuccessJQuery,
        error: OnFailureJQuery
    });
}

Hope this help you get going faster!

Posted in .net, JavaScript, web services | Tagged , , | Leave a comment

Outlook Unable to Start With an Exchange Account

I had set up a new Exchange Server 2010, and was trying to connect to my account on that server using Outlook 2010. However, I kept getting this error and Outlook was unable to start.

"Cannot open your default e-mail folders. You must connect 
to Microsoft Exchange with the current profile before you 
can synchronize your folders with your offline folder file."

The computer I was using was in the same Windows Domain as the Exchange Server, and I was able to access (remote desktop) the Exchange Server without any issues. I was also able to access my account using the OWA interface without any issues. The only problem was that I could not access it via Outlook.

There are a plethora of blog posts and forums online where users have experienced the exact same error message and many different solutions have been suggested. None of them worked for me until I finally found this one.

While the solution proposed in the post itself did not work for me, one of the comments on the post proposed a solution that did. Waseel Akbari had the following comment:

It worked once for me but later the same problem started again, after too much struggle I found a solid solution and that is:
Go to Exchange server ::> Services ::> Check if this service is started (Microsoft Exchange RPC Client Services) if it’s started give it a restart

This made the most sense to me given the error message and the fact that Outlook uses RPCs to communicate with Exchange Server (whether it is using MAPI RPCs or RPC over HTTPS). You can read this great article about Outlook Anywhere (which is another name for Outlook’s RPC over HTTPS functionality).

So I remote desktop’d into my Exchange server -> Start -> Run -> services.msc and looked for the “Microsoft Exchange RPC Client Access” service and sure enough, it was not running. I started it, and voila, I was able to connect to my Exchange account using Outlook.

Posted in network administration | Tagged , , | Leave a comment

Issues after installing Windows Server 2008 R2 on a laptop

After installing Windows Server 2008 R2 on my laptop, I noticed a couple of things:

  1. On closing the lid of the laptop, it did not go into sleep or hibernate mode, but was instead running and draining power
  2. I could not see the list of wireless connections available, and hence was not able to connect to one
  3. Internet Explorer was in High security level for the Internet zone and I could not change that

All these issues happen because the OS I installed was Windows Server 2008 R2. I guess the defaults associated with that server OS were not really applicable for a laptop.

Here are the fixes:

  1. The obvious place to look was power options inside the Control Panel. I intended to change the “When I close the lid” setting and chose the “Hibernate” option. However it was not an available option. What I had to do first is run the following on a command prompt
    c:\> powercfg.exe /hibernate on

    When I did this, it caused the Hibernate option to show up in the “When I close the lid” setting. I chose that and it just worked.

    This information was obtained from here.

  2. To fix the wireless issue, I ran Windows Diagnostics and like any user expected it not to work. It lived up to expectations. It told me that nothing was wrong. On looking at network adapters, the wireless adapter showed up as enabled. Finally based on this link, I came to know what the issue was. It was that Windows Server 2008 R2 (and probably other servers) doesn’t have wireless access turned on by default, and the “Wireless LAN Service” has to be explicitly started by going to
    Start -> Administrative Tools -> Server Manager -> Features

    and adding the feature with the same name.

    Don’t ask me why Windows Diagnostics would not just give the message “The Wireless LAN service is not running, start it for wireless access”. I’m confounded!

  3. It turns out that in Windows Server 2008 R2 puts Internet Explorer in Enhanced Security Configuration (also known as IE ESC) by default, and when in that configuration, you cannot alter the security setting, and will receive annoying messages whenever you navigate to any site that you have not explicitly added to the trusted list. Also you will probably not be able to download other software like Google Chrome :-). I found the solution here, which is to go to the Server Manager and click on the “Configure IE ESC” link on the right side of the window. Once you turn off ESC, you will automatically be put in “Medium-High” security setting for the Internet zone and can browse normally. Keep in mind that you are lowering the security setting of your machine and hence increasing the probability that your computer is infected with spam ware or a virus, so take the appropriate precautions like installing an antivirus and correctly configuring the firewall.

Anyway, hope this helps.

Posted in Uncategorized | Tagged , , , | Leave a comment

Mapping a local branch to a remote branch in GIT

If you’ve created a git repository using the following steps:

  1. Created a local git repository using “git init .”
  2. Created a remote repository on github.com or another host
  3. Added that remote to the local repository using “git add remote origin <path to remote repo>”

Then you might be in a state where your local master is not mapped to the remote master. To fix that run the following command:

git branch --set-upstream master origin/master

I suspect that in step 3 there could be a way to specify the mapping so it doesn’t have to be done later explicitly, but I’m not aware of it currently.

Posted in software development | Tagged , | Leave a comment

How to add a new database user based on a windows user

Here’s another post based on my gripes against database administration tools and how difficult they are to use (and the fact that so many people seem to be ok with it).

Many people have multiple windows users on their PCs. Maybe one for administration and one for daily usage for example. That’s my setup, I have a “laptop\admin” user and a “laptop\me” user, and recently I was trying to create a new database on my local SQL Express 2008 installation while logged in with my “laptop\me” account. I was using SQL Server Management Studio to do this. Off course, it did not work. Here is the error I got.

“CREATE DATABASE permission denied in database ‘master’. (Microsoft SQL Server, Error: 262)”

What that means is that my “laptop\me” user does not have the permissions to create a new database on my SQL Express instance. OK, no worries, I get that. I should be able to start SSMS as “laptop\admin” and grant the said permissions to “laptop\me”. No such luck. Nothing is that simple in database land.

I’ll cut to the chase and list the steps you need to follow to make that happen.

- Start SSMS as an admin user (right click and choose “Run as Administrator”)

This is assuming that the admin user has the authorization to create a new login and grant the create database permission. To do the database login associated with that admin user will need to be in the “sysadmin” role. More baloney I know! Someone in the team felt that every developer needs to be exposed to the full permissioning machinery just to create a database. If you do not have a login that is in the sysadmin role for the SQL Server instance  you are working with, unfortunately you have to create one. Click here for a link to a blog post that can help you do that. The post is for SQL Server but works for SQL Express also.

- Create a login in the SQL Express instance

USE master
CREATE LOGIN [laptop\me] FROM WINDOWS

- Create a user for that login

USE master
CREATE USER mydatabaseuser FOR LOGIN [laptop\me]

- Grant permission to that newly created user

USE master
GRANT CREATE DATABASE TO mydatabaseuser

Too many steps in my opinion, but hey, how would we have “database guys” if this was easy!!

Posted in database, sql | Leave a comment

Yui Compressor issues with reserved words in JavaScript

For the life of me, I could not figure out why I was getting an error while running the Yui compressor on a set of my JavaScript files.

The error was very generic and not helpful

error : Failed to read in the data for the path/file [..\<path>\<filename>]. The most common cause for this is because the path is incorrect or the file name is incorrect … so please check your path and file names. Until you fix this up, I can’t continue … sowwy.

The file was present and seemed to be syntactically correct and all.

Finally, thanks to a little squiggly placed by Resharper (which is awesome!!), I found the issue. One of the js functions was called “goto” and that, apparently is reserved by JavaScript for “future use”, and Yui bombs on it.

Renaming it fixed the issue.

UPDATE

To be clear, the YUI compressor is behaving as it was designed to. Reserved words should not be used in JavaScript, as in other languages. However, I will say that the error dumped out when the compressor runs into a reserved word could be better.

Posted in development tools, JavaScript, software development | Tagged | 3 Comments

Crazy Idea of the Day – Product Placement in Google Streeview

I was looking up the street view for the Met, and this is what the view from Columbus Avenue looks like. Fresh Direct guys are every where in NYC but not so sure this was unintentional! That’s some prime product placement for a great NYC Startup.

Just FYI, this is on 02/20/2012.

Posted in Uncategorized | Leave a comment