简体   繁体   English

Ajax - 500内部服务器错误

[英]Ajax - 500 Internal Server Error

I am trying to learn AJAX for this project at work. 我正在努力为这个项目学习AJAX。 I have a site that loads medications that a patient is taking. 我有一个网站,可以加载患者正在服用的药物。

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. 我以递归方式调用这个AJAX函数,以便它附加一个包含单一药物和7天历史记录的新表。 I am having issues getting the code to execute in FF and IE. 我在让代码在FF和IE中执行时遇到问题。 Works perfectly fine in chrome. 铬合金完美无缺。 I had alerts displaying the xmlhttp.status and this is what I got: 我有警告显示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. 如果需要更多代码,请告诉我。

The 500 (internal server error) means something went wrong on the server's side. 500 (internal server error)意味着服务器端出现了问题。 It could be several things, but I would start by verifying that the URL and parameters are correct. 这可能是几件事,但我首先要验证URL和参数是否正确。 Also, make sure that whatever handles the request is expecting the request as a GET and not a POST. 此外,请确保处理请求的任何内容都将期望请求作为GET而不是POST。

One useful way to learn more about what's going on is to use a tool like Fiddler which 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. 如果您没有令人信服的理由编写自己的Ajax代码,那么使用为您处理Ajax交互的库会更好。 jQuery is one option. jQuery是一种选择。

I think your return string data is very long. 我认为你的返回字符串数据很长。 so the JSON format has been corrupted. 所以JSON格式已经损坏。 You should change the max size for JSON data in this way : 您应该以这种方式更改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>

This may be an incorrect parameter to your SOAP call; 这可能是SOAP调用的不正确参数; 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. 查看'data:'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. 谷歌Chrome的调试工具栏有一些很好的工具来验证参数和查看错误消息 - 例如,从控制台选项卡开始,然后单击错误的URL或单击网络选项卡。 You will want to view the message's headers, response etc... 您将需要查看邮件的标题,响应等...

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 Service,请取消注释以下行。

[System.Web.Script.Services.ScriptService]

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

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 .asmx web 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'或其中一个dep .. “。

So log on the server and debug from there. 所以登录服务器并从那里进行调试。

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');

I had the same error. 我有同样的错误。 It turns out that the cause was that the back end method was expecting different json data. 事实证明,原因是后端方法期望不同的json数据。 In my Ajax call i had something like this: 在我的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. 那么问题是我的ajax调用和我的后端端点之间的映射是不一样的。 As soon as i changed my ajax call to the following, it all worked well! 一旦我将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调用中的数据变量才能完全匹配方法签名。

必须使用behavior: JsonRequestBehavior.AllowGet在Post Json Return C#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM