ASP.net通过Ajax传递JSON字符串在本地有效,但在Windows Azure服务器上无效

时间:2022-06-28 01:41:54

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.

我的项目的主要问题是,我的代码不能使用Windows Azure Server,但它是本地的。我已经找了两天了,和两天前一样困惑。让我们看看你们是如何处理这个问题的。

At first my JavaScript code:

首先,我的JavaScript代码:

    $("#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.

使用Ajax调用,我希望将收集到的数据传递到ASP.net代码中。

ASP.net code:

ASP.net代码:

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:

我的“formula are_einsatprotokoll”类应该很简单:

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:

我的程序运行良好,直到我将它上传到我的Windows Azure平台。突然,它决定我的代码是错误的,并给我以下错误信息:

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 * won't allow me posting pictures until I got at least 10 reputations.)

(我本来可以把它作为图片发布的,但*不允许我发布图片,直到我获得至少10个声誉。)

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?

在服务器上,我的Ajax并没有将数据传递给ASP.net,所以我的字符串是空的。但是为什么呢?为什么它只在本地运行,而不在服务器上运行?

1 个解决方案

#1


1  

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.

首先:我的问题既不是JSON也不是Ajax。当我在开发人员控制台的谷歌chrome上收到错误消息时,我总是遵循错误链接。在我的例子中"firepad.azurewebsites.net/operation/ErstelleProtokoll”。但当我调用这个URL时,我回忆起了函数JSON。使用空值反序列化:当然,因为在回忆中没有传递值。

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.

在我将这个版本上传到服务器后,我发现Azure服务器上有一个路径异常,这是由于根符号(“~”)引起的。所以我开始在MemoryStream类中处理这个异常。经过几次努力,我找到了解决这个问题的正确方法。

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

我的问题不是Ajax调用,而是反序列化后的代码行。这句话引起了问题,因为我从一个有“工作保证”的同事那里得到了这段代码,我从来没有这样尝试过。所以信任并不总是有用的

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

感谢这个伟大的论坛,因为这里的问题解决了我之前的大部分错误。

#1


1  

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.

首先:我的问题既不是JSON也不是Ajax。当我在开发人员控制台的谷歌chrome上收到错误消息时,我总是遵循错误链接。在我的例子中"firepad.azurewebsites.net/operation/ErstelleProtokoll”。但当我调用这个URL时,我回忆起了函数JSON。使用空值反序列化:当然,因为在回忆中没有传递值。

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.

在我将这个版本上传到服务器后,我发现Azure服务器上有一个路径异常,这是由于根符号(“~”)引起的。所以我开始在MemoryStream类中处理这个异常。经过几次努力,我找到了解决这个问题的正确方法。

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

我的问题不是Ajax调用,而是反序列化后的代码行。这句话引起了问题,因为我从一个有“工作保证”的同事那里得到了这段代码,我从来没有这样尝试过。所以信任并不总是有用的

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

感谢这个伟大的论坛,因为这里的问题解决了我之前的大部分错误。