Jquery AJAX Post: 500 (Internal Server Error)?

2020-07-25 01:28发布

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

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

Here is my ajax code:

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 ?

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?

2条回答
疯言疯语
2楼-- · 2020-07-25 01:39

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.

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.

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

Page.Server.ScriptTimeout = 300;
查看更多
狗以群分
3楼-- · 2020-07-25 01:54

Your error come from your data parameter. Stringify data object instead of { '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 param1 parameter.

查看更多
登录 后发表回答