Benedikt Berger
Benedikt Berger

Reputation: 31

ASP.net passing JSON string via Ajax works local but won't work on Windows Azure Server

My main problem with my project is, that my code won't work with Windows Azure Server but it does local. I have been searching for two days now and I am as confused as two days ago. But let's see how you guys would manage this issue.

At first my JavaScript code:

    $("#btnsave").click(function () {

    var prot = [];

    for (var i = 1; i <= zeilencounter; i++) {
        var val = { "Zeit": String($("#protokoll_" + i + "_zeit").val()), "Protokolltext": $("#protokoll_" + i + "_text").val(), "Erledigt": $("#protokoll_" + i + "_check").prop("checked") };

    prot.push(val);

    }
    var protokoll = { "Protokoll": JSON.stringify(prot) };

    $.ajax({
        type: "POST",
        url: '/operation/ErstelleProtokoll/',
        cache: false,
        data: protokoll,
        dataType: 'html',
        success: function (result) {
            hidePopups();
            SaveInfo();
            window.location.assign('http://firepad.blob.core.windows.net/einsatzprotokoll/einsatzprotokoll.pdf');
        }
    });

You see it's not really complicated. The only things I do is to collect data from my protocol form.

With the Ajax call I want to pass the collected data to my ASP.net code.

ASP.net code:

public void ErstelleProtokoll(string protokoll)
    {
        Formulare_Einsatzprotokoll[] Einsatzprotokoll = JsonConvert.DeserializeObject<Formulare_Einsatzprotokoll[]>(protokoll);

        new FirePad.Services.Einsatzprotokoll(Einsatzprotokoll);
    }

My "Formulare_Einsatprotokoll" class simple as it should be:

public class Formulare_Einsatzprotokoll
{
    public string Zeit { get; set; }
    public string Protokolltext { get; set; }
    public bool Erledigt { get; set; }

    public Formulare_Einsatzprotokoll()
    {

    }
}

(I have a constructor because somewhere on the internet i read, that this may solve my problem.)

My program works fine until I upload it to my Windows Azure platform. Suddenly it decides that my code is wrong and give me the following error message:

Value cannot be null.
Parameter name: value

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: value

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentNullException: Value cannot be null.
Parameter name: value]
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) +161
   Newtonsoft.Json.JsonConvert.DeserializeObject(String value, JsonSerializerSettings settings) +66
   firepad.Controllers.OperationController.ErstelleProtokoll(String protokoll) +206
   lambda_method(Closure , ControllerBase , Object[] ) +104
   System.Web.Mvc.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +182
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +99
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +50
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +55
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9515512
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

(I would have posted it as a picture, but stackoverflow won't allow me posting pictures until I got at least 10 reputations.)

On the server it seems, that my Ajax doesn't pass data to ASP.net, so my string is null. But why? Why it works local but not on the server?

Upvotes: 2

Views: 854

Answers (1)

Benedikt Berger
Benedikt Berger

Reputation: 31

In case somebody will ever need exactly this: here is your solution.

At first: my problem was neither JSON nor Ajax. When I got my error message on google chrome in the developer console i always followed the error link. In my case "firepad.azurewebsites.net/operation/ErstelleProtokoll". But when I called this URL I recalled the function JSON.Deserizalize with a null value: of course, because in the recall there are no values passed with.

So I started rethinking my problem and got the idea the error may be caused through the:

new FirePad.Services.Einsatzprotokoll(Einsatzprotokoll);

I commented the following lines out of my code:

Formulare_Einsatzprotokoll[] Einsatzprotokoll = JsonConvert.DeserializeObject<Formulare_Einsatzprotokoll[]>(protokoll);

After I uploaded this version to the server, I recognized that there is a path exception on the Azure Server caused due to the root symbol ("~"). So I started working around this exception with the MemoryStream class. After a few attempts I got the right solution for the problem.

So I reused the:

Formulare_Einsatzprotokoll[] Einsatzprotokoll = JsonConvert.DeserializeObject<Formulare_Einsatzprotokoll[]>(protokoll);

and never had the error again. You should have seen my face - that was quite brilliant.

Because it's such a beautiful story - summarize it up:

My problem wasn't the Ajax call, it was the code-line after my deserialization. This line caused the problem and because I got this code from a colleague with a "working guarantee", I have never tried it this way. So trusting is not alway useful :D

Thanks anyway to this great forum, because the questions in here fixed most of my errors before.

Upvotes: 1

Related Questions