Javascript Jquery AJAX 帖子:500(内部服务器错误)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36611400/
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 19:17:12  来源:igfitidea点击:

Jquery AJAX Post: 500 (Internal Server Error)?

javascriptjqueryajaxweb-servicesgoogle-chrome-extension

提问by user5535577

I am trying post a string to web service but I am getting this error (Google Chrome Extension Project):

我正在尝试将字符串发布到网络服务,但出现此错误(Google Chrome 扩展项目):

jquery-2.1.1.min.js:4 POST http://localhost:49242/Service.asmx/test500 (Internal Server Error)

jquery-2.1.1.min.js:4 POST http://localhost:49242/Service.asmx/test500(内部服务器错误)

Here is my ajax code:

这是我的ajax代码:

var data = {};
data.param1 = words[0];

$.ajax({
    data: JSON.stringify({ 'data': data.param1 }),
    dataType: 'application/json',
    url: 'http://localhost:49242/Service.asmx/test',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    success: function (result) {
        alert(result);
    },
    failure: function (errMsg) {
        alert(errMsg);
    }
});

My service:

我的服务:

[WebMethod]

[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public string test(string param1) {
    return param1;
}

I am working on this problem about 3 days. Can you help me ?

我正在解决这个问题大约 3 天。你能帮助我吗 ?

By the way, I have a question. I am posting json variable to service with ajax(like you see), but service returning xml value. Is there a problem or [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]this code block solving problem?

顺便说一下,我有一个问题。我正在使用 ajax 将 json 变量发布到服务(如您所见),但服务返回 xml 值。是否有问题或[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]此代码块解决问题?

回答by Fabien PERRONNET

Your error come from your data parameter. Stringify dataobject instead of { 'data': data.param1 }:

您的错误来自您的数据参数。字符串化data对象而不是{ 'data': data.param1 }

var data = {};
data.param1 = words[0];

$.ajax({
    data: JSON.stringify(data),
    dataType: 'application/json',
    url: 'http://localhost:49242/Service.asmx/test',
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    success: function (result) {
        alert(result);
    },
    failure: function (errMsg) {
        alert(errMsg);
    }
});

Your stringifyed data will result in {"param1":"Words"}, then your service should be able to bind the param1parameter.

您的字符串化数据将导致{"param1":"Words"},那么您的服务应该能够绑定param1参数。

回答by Jitendra G2

I was facing this type of error on AJAX post response. I was spending too much time behind this issue and finally I caught it.

我在 AJAX 发布响应中遇到了这种类型的错误。我在这个问题上花了太多时间,最后我抓住了它。

It throws a 500 internal error because the AJAX response has a lot of content from the server so it returns a timeout of execution.

它抛出一个 500 内部错误,因为 AJAX 响应有很多来自服务器的内容,因此它返回执行超时。

So I just added the line below and it's working fine.

所以我只是添加了下面的行并且它工作正常。

Page.Server.ScriptTimeout = 300;