Amazon Ad

Monday 26 May 2014

jQuery ajax POST and saving value in local javascript variable


Hi Guys,

I was working on an ASP.NET page and found a strange problem. Whenever i
was calling the jquery ajax POST request, The data from webservice was
available in the success function but not after $.ajax request code. This
was strange i as wanted to access the data after calling the ajax POST
request.

Here is the problem

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

After executing the above it gave me "Inside Success Hello", But the second
alert message gave me "After ajax undefined". This was due to the fact
that the first call was made inside success which was asynchronous, Whereas
the second alert was not asynchronous and didn't wait until the
$.ajax POST request executed.

To solve the same, I made it synchronous as i had to store the result of
webservice in a local variable, Here is the code

var localdata;
$.ajax({
data:"{'id':'1'}",
type:"POST",
dataType:"json",
        async:false, //setting this made it a synchronous call
contentType:"application/json",
url:"http://www.test.com/test.svc/HelloWorld",
success:function(data){localdata=data;alert("Inside Success "+localdata);},
error:function(a,b,c){}
});
alert("After ajax "+localdata);

Thanks
Ritesh Tandon

Thursday 1 May 2014

Creating Asynchronous Thread in Global.asax in MVC

Hi guys,

I was facing a problem of creating asynchronous thread in Global.asax in MVC. I was able to create a thread which runs asynchrounsly without affecting the application. Here is how how i did it in global.asax.cs file. It also requires two namespaces 1. System.Threading 2. System.Threading.Tasks, Please do add them.

Step 1: Create a async Task, here in this example its MyThread.

private async Task<int> MyThread()
        {
            while (true)
            {
                string result = await Operations();
                await Task.Delay(5000);
            }
            return 1;
        }

Step 2: Create another async Task performing all the operations.

public async Task<String> Operations()
        {
            return "Hello";
        }
Step 3: Inside Application_Start call the thread.

protected async void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            AllSync();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

Thanks
Ritesh Tandon

How to implement Captcha v3 in ASP.NET

 I was facing an issue of dom parsing in my website. I finally resolved it by using Google Captcha V3. Step 1: Get your keys from https:...