[英]Ajax - 500 Internal Server Error
我正在努力为这个项目学习AJAX。 我有一个网站,可以加载患者正在服用的药物。
我以递归方式调用这个AJAX函数,以便它附加一个包含单一药物和7天历史记录的新表。 我在让代码在FF和IE中执行时遇到问题。 铬合金完美无缺。 我有警告显示xmlhttp.status,这就是我得到的:
xmlhttp.status == 500(内部服务器错误)。
我注释掉了所有的递归,因此它缩小到了这个代码。 (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;
}
如果需要更多代码,请告诉我。
我认为你的返回字符串数据很长。 所以JSON格式已经损坏。 您应该以这种方式更改JSON数据的最大大小:
打开Web.Config文件并将这些行粘贴到配置部分
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
这可能是SOAP调用的不正确参数; 查看'data:'json部分中参数的格式 - 这是您传递的有效负载 - 以JSON格式包装的参数和数据。
谷歌Chrome的调试工具栏有一些很好的工具来验证参数和查看错误消息 - 例如,从控制台选项卡开始,然后单击错误的URL或单击网络选项卡。 您将需要查看邮件的标题,响应等...
取消注释以下行: [System.Web.Script.Services.ScriptService]
服务将开始正常运作。
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
要允许使用ASP.NET AJAX从脚本调用此Web Service,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
有同样的问题,那么我记得,出于安全原因,ASP在远程访问您的站点/服务时不会暴露整个错误或堆栈跟踪,就像无法远程测试.asmx
Web服务一样,所以我远程进入服务器和监视我的开发工具,然后才得到臭名昭着的消息“无法加载文件或程序集'Newtonsoft.Json, Version=3.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
或其中一个dep .. “。
所以登录服务器并从那里进行调试。
我修改了一个像这样的错误来改变routes.php中路由的位置,例如我有这样的东西:
Route::resource('Mensajes', 'MensajeriaController');
Route::get('Mensajes/modificar', 'MensajeriaController@modificarEstado');
然后我这样说:
Route::get('Mensajes/modificar', 'MensajeriaController@modificarEstado');
Route::resource('Mensajes', 'MensajeriaController');
我有同样的错误。 事实证明,原因是后端方法期望不同的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",
});
现在在我的WebMethod中,在我的C#后端代码中,我已经声明了我的端点:
public static string PostAjax(AjaxSettings settings)
宣布AjaxSettings的地方:
public class AjaxSettings
{
public string url { get; set; }
}
那么问题是我的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"
});
我必须更改Ajax调用中的数据变量才能完全匹配方法签名。
必须使用behavior: JsonRequestBehavior.AllowGet
在Post Json Return
C#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.