Zhixian's Tech Blog

2015-02-09

done function not called in jQuery AJAX call

Filed under: development, web application development — Tags: , , — Zhixian @ 23:40:02 pm

In jQuery, you can write an AJAX call that is chained to a done function.
Sometimes this function does not get called.

One of things to examine is the response that you are receiving from the server.
If the response is malformed, the done function will not get triggered.

An example of a malformed response:

{ "result" : OK }

Correct reponse (note the presence of quotation marks around OK:

{ "result" : "OK" }

2015-01-10

Can not load type MembershipPasswordAttribute using ASP.NET (Mono)

Filed under: web application development — Tags: , , , — Zhixian @ 08:33:01 am

Note: This blog post does not provide a solution.

Background

Recently, I was trying to build a ASP.NET MVC project using Mono on my Ubuntu machine.
It did not end well.

While building the login page, I got this error message.

Could not load type ‘System.Web.Security.MembershipPasswordAttribute’ from assembly ‘System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

Error 500 - Google Chrome_201

The offending code is:

test.mvc.web - Views-Home-Index.cshtml - MonoDevelop_202

As it turns PasswordFor is not the only helper extension that will trigger this error.
TextBoxFor have the same issue as well.

Why this happens

As it turns out, the Mono 4.5 that I am using at this time of writing does not have this implemented yet.

You can view here (http://go-mono.com/status/status.aspx?reference=4.5&profile=4.5&assembly=System.Web).

Mono System.Web in 4.5 vs MS.NET 4.5 - Google Chrome_203

Solution

No good solution to this issue currently.

One possible workaround is not to use the HTML Helper extensions.

2014-12-28

How to install Mono on Ubuntu

Filed under: web application development — Tags: , , , , , — Zhixian @ 01:29:12 am

This blog post describes my installation steps to get Mono running on my Ubuntu.

I will write another blog post on testing this installation.

Assumptions

  1. You have Apache HTTP Server installed
  2. You know how to start a terminal session and run commands there.

Summary

  1. Add Mono Project GPG signing key
  2. Add Mono Package Repository
  3. Add mod_mono Repository
  4. Update package cache
  5. Install Mono
  6. Install mod_mono
  7. Enable mod_mono
  8. Reference

Mono Project GPG signing key

This step adds the GPG signing key to your key-ring.
This signing key is used to make sure the installed files are valid.
To add the key to your key ring, start a terminal session and run the following command:

sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF

zhixian@SARA: ~_120

Add Mono Package Repository

This step add the Mono package repository into apt-get list of available package repositories.
Enter the following command at the command-line of the terminal session:

echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list

zhixian@SARA: ~_121

Add mod_mono Repository

This step adds the repository for mod_mono to apt-get list of available package repositories.
mod_mono is use by Apache HTTP server to interpret .NET web pages.
To add the repository, run the following command at the command-line:

echo “deb http://download.mono-project.com/repo/debian wheezy-apache24-compat main” | sudo tee -a /etc/apt/sources.list.d/mono-xamarin.list

zhixian@SARA: ~_124

Update package cache

To update the apt-get package cache to use the repositories that you just added, run the following command at the command-line:

apt-get update

zhixian@SARA: ~_125

Install Mono

To install the complete Mono package enter the following at the command-line:

sudo apt-get install mono-complete

zhixian@SARA: ~_126

After the initial listing of the packages that will be installed, you will be given a prompt asking if you would like to proceed install Mono. Enter “Y” to proceed with the installation process.

zhixian@SARA: ~_127

zhixian@SARA: ~_128

Install mod_mono

Run the following command to add mod_mono:

sudo apt-get install libapache2-mod-mono

zhixian@SARA: ~_129

You might see an error exit status at the end of the installation process.
This is due to some configuration issue in the installation process.
I’m not too sure what exactly is the issue but it seems fine to ignore it.

zhixian@SARA: ~_131

Enable mod_mono

mod_mono is enabled by running the following command:

sudo a2enmod mod_mono

zhixian@SARA: ~_133

Reference

http://www.mono-project.com/docs/getting-started/install/linux/

https://help.ubuntu.com/community/ModMono

2014-12-21

Google App Engine development on Ubuntu for Python (Part 2 of 2)

Filed under: web application development — Tags: , , , , , — Zhixian @ 23:46:12 pm

This blog post describes how to run Google App Engine (GAE for short) on Ubuntu.
This is the second part of a 2-part blog post.
This blog post focus on writing a simple (helloworld) application just to demonstrate that the setup works and that we can deploy our application.

Parts

  1. Setting up Google App Engine SDK
  2. Running Google App Engine (this article)

An aside related article, How to Add Google App Engine project to Google Developers Console.

Assumptions

This blog post builds on the previous blog post Setting up Google App Engine SDK and assumes that you have followed the instructions there.

This blog post also assumes you know how to register a Google App Engine project in the Google Developers Console.
If you need help with this, you can take a look at my blog post, How to Add Google App Engine project to Google Developers Console.

Part 2 Summary

  1. Create project folders (opinionated)
  2. Create request handler
  3. Create configuration file
  4. Test project
  5. Deploying project

Create Project Directory

(Opinionated) I usually store my projects in a Projects folder in my home folder.

zhixian_084

Inside this Projects folder, right-click the folder area to display the context menu.
Click Create Folder from the context menu.

zhixian@SARA: ~-Projects_085

Name the folder HelloWorld.
So your Projects folder, should look like the below.
The files (request handler and configuration) will be stored inside the HelloWorld folder that you have created.

Projects_086

Create Request Handler

Double-click the HelloWorld folder to open the folder.
Right-click in the folder area to display the context menu.
Click Empty File from the context menu.

HelloWorld_087

Name the file as

HelloWorld.py

Your HelloWorld folder should look like the below:

HelloWorld_088

Right-click the HelloWorld.py file that you just created to display its context menu.
Click Open with Text Editor from the context menu to edit the file.
This will open the file in your configured text editor. By default, this should be gedit.

HelloWorld_090

Copy and paste the below into the file.

import webapp2

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')

application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

After you paste the above content into the file, it should look like the below.
Note the amount of space used for indentation.
This is important in Python as indentation is used to indicate code blocks.

Click on Save on the toolbar to save the file.

HelloWorld.py (~-Projects-HelloWorld) - gedit_092

Create Configuration File

In your HelloWorld folder, right-click in the folder area to display the context menu.
Click Empty File from the context menu.

HelloWorld_093

Name this file

app.yaml

HelloWorld_095

Right-click the app.yaml file that you just created to display its context menu.
Click Open with Text Editor from the context menu to edit the file.

HelloWorld_096

Copy and paste the below into the file, replacing the your-app-id with the project id that you defined in your Google Developers Console.

application: your-app-id
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: HelloWorld.application

Now, you are ready to test the project.

Test Project

To test your project, open a terminal session and go to the folder that holds your project folder.
For my example, my HelloWorld project is created in a folder call Projects in my home folder.
So I would type the following command after starting the terminal session.

cd Projects

If I list the contents of the Projects folder, it should look like the below:

zhixian@SARA: ~-Projects_105

To run the HelloWorld application,

  1. Enter the following at the command prompt:

    dev_appserver.py HelloWorld/

  2. You will be prompt to give permission for the dev_appserver script to check for updates on startup.
    Enter “n” to disallow this check. Leaving this as “Y” or blank will allow dev_appserver to check for updates.
  3. Note the url of the “default” module.
    This is the url that your application is being hosted at.
    By default it is:

    http://localhost:8080

  4. Note the url of the admin server.
    This is the url that of the local testing administration server to your application.
    By default it is:

    http://localhost:8000

zhixian@SARA: ~-Projects_104

If you open your browser and navigate to the url of the “default” module (http://localhost:8080), you should see:

Firefox Developer Edition_107

If you open your browser and navigate to the url of the admin server (http://localhost:8000), you should see:

Instances - Firefox Developer Edition_108

To stop the test server, press <Control>-<C> keys combination.

zhixian@SARA: ~-Projects_109

Deploy Project

To upload the project to Google:

  1. Enter the following command at the command prompt:

    appcfg.py update HelloWorld/

  2. Enter your Google (gmail) account.
  3. Enter the password to your account.
  4. After the update has complete, you should see “Deployment Success”, somewhere near the end.

zhixian@SARA: ~-Projects_110

After you have deployed the application, you can access it at:

http://<project-id>.appspot.com/

Firefox Developer Edition_111

Reference

  1. Hello, World! – Python – Google Cloud Platform
  2. Uploading Your Application – Python – Google Cloud Platform

How to Add Google App Engine project to Google Developers Console

This blog post describes how to create add Google App Engine project.

Assumption

You have a Google account.

Summary

  1. Log in to Google Developers Console
  2. Create Project

Log in to Google Developers Console

In your web browser, navigate to Google Developers Console (https://console.developers.google.com/)

Google Developers Console - Firefox Developer Edition_097

After you logged in, you should be redirected to your project page.

Create Project

Click Create Project to display the new project.

Google Developers Console - Firefox Developer Edition_098

On the New Project dialog, enter:

  1. A name for your project.
    For example, I am naming my hello world project as zx-helloworld.
  2. An id for your project.
    Important This id will be used for the deployment of your project.
    For example, I am naming my hello world id as zx-helloworld.
  3. Click Create to add the project.

Note

The project id needs meet the follow criteria:

  1. Starts with a lower-case letter (a-z)
  2. May contains lower-case letters, digits (0-9) or hyphen (-)

Google Developers Console - Firefox Developer Edition_099

After you clicked Create, the dialog will close.
You should see a small popup at the lower right-hand corner of the page.

Google Developers Console - Firefox Developer Edition_100

The popup should indicate that its creating the project.

Google Developers Console - Firefox Developer Edition_100

After the project is created, it should redirect you to the project dashboard.

Google Developers Console - Firefox Developer Edition_101

The popup in the lower right corner of the page, should have a green checkbox.

Google Developers Console - Firefox Developer Edition_102

With this, your Google App Engine project can be deployed using the project id that you defined.

2014-12-02

How to install PHP to Apache HTTP Server in Ubuntu 14.04 (Trusty Tahr)

Filed under: php, web application development — Tags: , , , , , , — Zhixian @ 10:36:12 am

This blog post describes how I install PHP to Apache HTTP Server and test the installation on my Ubuntu installation.

Steps

  1. Installation
  2. Verifying PHP5 Modules in Apache2 HTTP Folder
  3. Testing installation

Installation

The quick and simple way is to use Ubuntu Software Center (search term: php5).
This will install the latest version of PHP5 available in the Ubuntu Software Center.

Ubuntu Software Center_010

Verifying PHP5 Modules in Apache2 HTTP Folder

After the installation is complete, you should see Apache HTTP modules for PHP in the mods-enabled folder.
By default (if you installed Apache HTTP server via Ubuntu Software Center), the installation folder is located at:

/etc/apache2

In the installation folder, there should be a folder labeled mods-enabled.
In this folder, you should see a php5.conf (that holds your configuration settings for PHP5) and php5.load (the actual Apache2 module for PHP5) file.

zhixian@SARA: -etc-apache2-mods-enabled_011

Testing installation

After you confirmed that PHP module is enabled in PHP, you can test your installation.
To do so, create a test.php file in Apache’s document root folder.
By default, the document root folder is located in /var/www/html.
However, the owner and group access for this folder belongs to the root user.

html Properties_013

This means you need to access the /var/www/html folder as a superuser in order to make changes to the folder.
One way would be start a terminal session and enter the following commands:

gksudo caja /var/www/html &

zhixian@SARA: ~_015

If you are not using Mate desktop, replace “caja” with “nautilus” as in:

gksudo nautilus /var/www/html &

This command can be explained as follows:

gksudo – run command as superuser. This is similar to the sudo command. But because we are running the command in the background (see & below), the prompt to allow us to enter administrator’s credentials will be in the background as well. By using gksudo, we will get a prompt like the below, that allows us to enter administrator’s password.

Workspace 1_016

caja (or nautilus) – command to activate your file manager

/var/www/html – parameter used by file manager. File manager use this parameter and open this folder when it start.

& – The ampersand indicates that this command is to run in the background. If this is missing, the terminal session will be dedicated to run the file manager until the file manager application terminated.

After you run the command, you should see:

html (as superuser)_017

In this folder, create a file call test.php with the following contents:

<html>
<head>
<title>PHP Test Page</title>
</head>
<body>
<h2>A PHP test page</h2>
<?php echo “hello world”; ?>
</body>
</html>

test.php (-var-www-html) - gedit (as superuser)_018

Now, your /var/www/html folder should look something like this:

html (as superuser)_020

Go to your browser and navigate to:

http://localhost/test.php

You should see a result like the below:

PHP Test Page - Google Chrome_021

2014-09-01

Running Classic ASP on IIS in Windows 7

Filed under: web application development — Tags: , , , — Zhixian @ 00:06:09 am

This blog post describes how to enable IIS7 in WIndows 7 to run classic ASP scripts.

Assumptions

This guide assumes:

  1. You know how to go to Windows 7 Control Panel.
  2. You know how to go to Internet Information Services (IIS) Manager

Steps to enabling ASP on IIS

1. In the Control Panel, click the item labeled “Programs and Features”.

image

2. In the Programs and Features window, click on the option “Turn Windows features on or off”.

image

3. In the Windows Features dialog, scroll down until you reach the item “Internet Information Services”.
Click the plus sign next to item label to display the menu items under “Internet Information Services”.
Repeat the same steps for “World WIde Web Services” under “Internet Information Services”.
Repeat again for “Application Development Features” under “World Wide Web Services”.

In the list of items under “Application Development Features”, is “ASP”.
Checked the box next to “ASP”.
Click on the “OK” button to apply changes.

image

After you clicked on the “’OK“ button, Windows will proceed to install the classic ASP extension into IIS.

image

After installation is complete, Windows will return you back to the Programs and Features window.

image

Verify installation

If your Internet Information Services (IIS) Manager happens to be open, close it.
If you open your IIS manager, you should see a menu item labeled ASP at server node.
image

Clicking on this item will allow you to configure settings for your classic ASP runtime.

2014-08-31

ASP.NET HTTP 503

Filed under: web application development — Tags: , , , , , , — Zhixian @ 22:47:08 pm

When working in ASP.NET, your web site that had been working fine might suddenly give you the below screen:

image

You are most likely to be receiving this error message because the application pool had stopped.

image

Your web site should start up again after you start the application pool.

image

Web site after application pool was started.

image

2014-08-09

Undefined function sqlsrv_connect()

One of the issues with using Microsoft Drivers 3.0 for PHP is it only works up to PHP version 5.4 while the latest stable version is 5.5.
Attempting to connect to a Sql Server database using the driver on a PHP 5.5 runtime will result in the following message.

image

Some workarounds would be:

  1. Use ODBC instead of Microsoft Drivers 3.0 for PHP.
  2. Downgrade the your PHP runtime from 5.5 to 5.4
  3. Get the source code for Microsoft Drivers 3.0 for PHP and compiled it for PHP 5.5 runtime.
  4. Get one of the re-compiled Microsoft Drivers 3.0 for PHP for PHP 5.5 that are distributed by others in the Internet.

2013-06-23

How to connect to Microsoft Sql Server from Ubuntu using pyODBC

This is continuation of my previous 3-part blog post that list out the steps carried out to establish a connection to Sql Server via ODBC on Ubuntu.

Assumptions

  1. The version of Ubuntu used in this guide is Ubuntu 12.04.2 LTS (Precise Pangoline) 32-bit.
  2. unixodbc is installed on the Ubuntu system.
  3. freetds is installed on the Ubuntu system.
  4. You are trying to connect to Microsoft Sql Server 2008 or later.
  5. You know how to use a terminal session in Ubuntu.

Required materiels

  1. pyODBC (latest version is 3.0.6 as of writing)
    (downloadable from http://code.google.com/p/pyodbc/downloads/list)

Download source vs apt-get

The apt-get utility in Ubuntu does have a version of pyODBC. (version 2.1.7)
However, it is badly out-of-date (2.1.7 vs 3.0.6) and may not work well with the newer versions of unixODBC and freetds.
This is especially important if you are trying to connect to later versions of Microsoft Sql Server (2008 onwards).
It is recommended that you use the latest versions of unixODBC, freetds and pyODBC when working with the latest Microsoft Sql Server instead of relying on packages in apt-get.

PyODBC Installation Overview

The steps for installing pyODBC are as follows:

  1. Download the source zip file. (I’m assuming you know how to do this)
  2. Extract the contents of the package.
  3. Install the contents of the package.
  4. Configure your system environment variables.

Extracting contents from pyODBC zip file

1) Assuming you downloaded the pyODBC zip into the Downloads folder of your Home directory, you should see a screen like the below on your file browser.

image_thumb[1]

2) To extract the contents of the package, right-click on it. A popup menu should appear.
Select the menu item “Extract Here”.

image_thumb[3]

3) After you extracted the contents, your file browser should look like the below:

image_thumb[5]

 

Installing pyODBC

1) Start a new terminal session.
Navigate to the location where you extracted the pyODBC source files by typing cd ~/Downloads/pyODBC-3.0.6/ at the command line.

image

2a) Proceed to install pyODBC by typing python ./setup.py install on the command line.

image

If you received a screen like the below, it means that your user account do not have the necessary permissions to install pyODBC onto the system.
Type sudo python ./setup.py install on the command line to use a superuser account to perform the installation.

image

3) If the installation succeed, you should see a screen like the below:

image

 

Configure system environment variables

pyODBC has a dependency on the file libodbc.so.
This is the library of functions that pyODBC use to perform ODBC operations.
If pyODBC cannot find this file, you may see the following message:

ImportError: <libodbc.so name> cannot open shared object file: No such file or directory

The actual name <libodbc.so name> may varied from system to system. A sample screen is as follows:

image

To help pyODBC find the library, we need to declare an environment variable LD_LIBRARY_PATH and specify the location of the libodbc.so in this variable.

Assuming my libodbc.so is located at  the directory /usr/local/lib

1) Type the following at the command line, to set the variable:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

image

2) Type the following to set the variable as a global variable so that the executing environment can access this variable.
export LD_LIBRARY_PATH

image

 

[Optional]

The changes made in the previous step are not persistent.
This means you need to carry out the same steps every time you start a new terminal session.

To make this change persistent, I recommend you put these 2 commands in your shell’s startup file.
If you are using the default bash shell, this would be your ~/.bashrc file.
Put the following 2 lines at the bottom of the file and save the file.

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib
export LD_LIBRARY_PATH

 

Testing pyODBC

For testing pyODBC, I have the following python script:

import pyodbc

database = pyodbc.connect('DRIVER=FreeTDS;SERVER=SARA\\SQLEXPRESS;UID=dbadmin;PWD=password;DATABASE=Arbalest;TDS_Version=8.0;',unicode_results=True)
cursor = database.cursor() encoding = 'utf-8' sql = "SELECT * FROM Arbalest.dbo.test;" cursor.execute(sql) for row in cursor:     print row

image

This python script assumes:

  1. I am trying to connect to the “SQLEXPRESS” Sql Server instance on a machine with the hostname of SARA
  2. The user account that I am using to log into the database is “dbadmin”
  3. The password to my user account is “password”
  4. The database that I am connecting to is “Arbalest”

For Sql Server later than 2005, having the parameter TDS Version=8.0 is recommended.

If this parameter is not defined in the connection string, the pyODBC may work correctly.

Assuming I save this script to a file call “test.py”, I would be able to run the script by typing the following on the command line:

python test.py

image

Note that the connection string that I used above in the pyodbc.connect statement is what is referred to as DSN-less connection string.

With this you should be able to connect to Sql Server from python using pyODBC.

Older Posts »

Create a free website or blog at WordPress.com.