Javascript Ajax - 500 内部服务器错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4789613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:06:17  来源:igfitidea点击:

Ajax - 500 Internal Server Error

javascriptajaxxmlhttprequest

提问by NickF

I am trying to learn AJAX for this project at work. I have a site that loads medications that a patient is taking.

我正在努力为这个项目学习 AJAX。我有一个网站,可以加载患者正在服用的药物。

I call this AJAX function up recursively so that it will append a new table containing a single medication and 7 days worth of history. I am having issues getting the code to execute in FF and IE. Works perfectly fine in chrome. I had alerts displaying the xmlhttp.status and this is what I got:

我递归地调用这个 AJAX 函数,以便它会附加一个包含单一药物和 7 天历史记录的新表。我在获取代码以在 FF 和 IE 中执行时遇到问题。在 chrome 中工作得很好。我有显示 xmlhttp.status 的警报,这就是我得到的:

xmlhttp.status==500 (internal server error).

xmlhttp.status==500(内部服务器错误)。

I commented out all of my recursion so it is narrowed down to this tid bit of code. ( x keeps track of the number of meds so i know when to stop)

我注释掉了我所有的递归,所以它被缩小到这一点代码。(x 跟踪药物的数量,所以我知道什么时候停止)

function LoadMeds()


  if ( x == MaxMedCount )
  {
      document.getElementById("the_day").value = parseInt(document.getElementById("the_day").value)+7; 
  }
  if ( x == (MaxMedCount - 1) )
  {
      document.getElementById("x").value = x + 1;
      show();
  }
  else
  { 

      if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
      }
      else
      {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
          try 
          {      
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
              {     
                  var div = document.createElement('div');
                  div.innerHTML= xmlhttp.responseText;
                  document.getElementById('MedTable').appendChild(div);
                  document.getElementById("the_med_").value = the_med_;

              }

          }
          catch(e)
          {
              alert("Error");  
          }
      }
      xmlhttp.open("GET","URL with variables passed",true);
      xmlhttp.send();     
      document.getElementById("x").value = x + 1;
  } 

if more code is needed just let me know.

如果需要更多代码,请告诉我。

回答by Jonathon Faust

The 500 (internal server error)means something went wrong on the server's side. It could be several things, but I would start by verifying that the URL and parameters are correct. Also, make sure that whatever handles the request is expecting the request as a GET and not a POST.

500 (internal server error)意味着服务器端出了点问题。可能有几件事,但我首先要验证 URL 和参数是否正确。此外,请确保处理请求的任何内容都期望请求为 GET 而不是 POST。

One useful way to learn more about what's going on is to use a tool like Fiddlerwhich will let you watch all HTTP requests and responses so you can see exactly what you're sending and the server is responding with.

了解有关正在发生的事情的更多信息的一种有用方法是使用像Fiddler这样的工具,它可以让您观察所有 HTTP 请求和响应,以便您可以准确地看到您正在发送的内容以及服务器正在响应的内容。

If you don't have a compelling reason to write your own Ajax code, you would be far better off using a library that handles the Ajax interactions for you. jQueryis one option.

如果您没有令人信服的理由来编写自己的 Ajax 代码,那么最好使用为您处理 Ajax 交互的库。 jQuery是一种选择。

回答by Emad Armoun

I think your return string data is very long. so the JSON format has been corrupted. You should change the max size for JSON data in this way :

我认为您的返回字符串数据很长。所以JSON格式已损坏。您应该以这种方式更改 JSON 数据的最大大小:

Open the Web.Config file and paste these lines into the configuration section

打开 Web.Config 文件并将这些行粘贴到配置部分

<system.web.extensions>
  <scripting>
    <webServices>
      <jsonSerialization maxJsonLength="50000000"/>
    </webServices>
  </scripting>
</system.web.extensions>

回答by user14114

This may be an incorrect parameter to your SOAP call; look at the format of the parameter(s) in the 'data:' json section - this is the payload you are passing over - parameter and data wrapped in JSON format.

这可能是 SOAP 调用的错误参数;查看“数据:”json 部分中参数的格式 - 这是您传递的有效负载 - 以 JSON 格式包装的参数和数据。

Google Chrome's debugging toolbar has some good tools to verify parameters and look at error messages - for example, start with the Console tab and click on the URL which errors or click on the network tab. You will want to view the message's headers, response etc...

谷歌浏览器的调试工具栏有一些很好的工具来验证参数和查看错误消息 - 例如,从控制台选项卡开始,然后单击出错的 URL 或单击网络选项卡。您将要查看消息的标题、响应等...

回答by Nadir Siddiqui

Uncomment the following line : [System.Web.Script.Services.ScriptService]

取消注释以下行: [System.Web.Script.Services.ScriptService]

Service will start working fine.

服务将开始正常工作。

[WebService(Namespace = "http://tempuri.org/")]



 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]



To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.

要允许使用 ASP.NET AJAX 从脚本调用此 Web 服务,请取消注释以下行。

[System.Web.Script.Services.ScriptService]

public class WebService : System.Web.Services.WebService 
{

回答by user2351772

Had the very same problem, then I remembered that for security reasons ASP doesn't expose the entire error or stack trace when accessing your site/service remotely, same as not being able to test a .asmxweb service remotely, so I remoted into the sever and monitored my dev tools, and only then did I get the notorious message "Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'or one of its dep...".

遇到了同样的问题,然后我记得出于安全原因,ASP 在远程访问您的站点/服务时不会公开整个错误或堆栈跟踪,就像无法.asmx远程测试Web 服务一样,所以我远程访问了服务器并监控我的开发工具,然后我才收到臭名昭著的消息“无法加载文件或程序集'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'或其中一个...”。

So log on the server and debug from there.

所以登录服务器并从那里调试。

回答by Muhammad Yasir

One must use behavior: JsonRequestBehavior.AllowGetin Post Json Returnin C#

必须在 C#behavior: JsonRequestBehavior.AllowGet中的 PostJson Return中使用

回答by Jennifervdz

I fixed an error like this changing the places of the routes in routes.php, for example i had something like this:

我修复了这样一个错误,改变了routes.php中路由的位置,例如我有这样的事情:

Route::resource('Mensajes', 'MensajeriaController');
Route::get('Mensajes/modificar', 'MensajeriaController@modificarEstado');

and then I put it like this:

然后我是这样写的:

Route::get('Mensajes/modificar', 'MensajeriaController@modificarEstado');
Route::resource('Mensajes', 'MensajeriaController');

回答by Sotiris Zegiannis

I had the same error. It turns out that the cause was that the back end method was expecting different json data. In my Ajax call i had something like this:

我有同样的错误。事实证明,原因是后端方法需要不同的 json 数据。在我的 Ajax 调用中,我有这样的事情:

$.ajax({
        async: false,
        type: "POST",
        url: "http://13.82.13.196/api.aspx/PostAjax",
        data: '{"url":"test"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
    });

Now in my WebMethod, inside my C# backend code i had declared my endpoint like this:

现在在我的 WebMethod 中,在我的 C# 后端代码中,我已经像这样声明了我的端点:

public static string PostAjax(AjaxSettings settings)

Where AjaxSettings was declared:

声明 AjaxSettings 的地方:

public class AjaxSettings
{
    public string url { get; set; }
}

The problem then was that the mapping between my ajax call and my back-end endpoint was not the same. As soon as i changed my ajax call to the following, it all worked well!

然后的问题是我的 ajax 调用和我的后端端点之间的映射不一样。一旦我将 ajax 调用更改为以下内容,一切都运行良好!

var data ='{"url":"test"}';    
$.ajax({
    async: false,
    type: "POST",
    url: "http://13.82.13.196/api.aspx/PostAjax",
    data: '{"settings":'+data+'}',
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

I had to change the data variable inside the Ajax call in order to match the method signature exactly.

我不得不更改 Ajax 调用中的数据变量,以便与方法签名完全匹配。