Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
152
rated 0 times [  159] [ 7]  / answers: 1 / hits: 62602  / 8 Years ago, wed, april 13, 2016, 12:00:00

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?


More From » jquery

 Answers
151

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.


[#62567] Tuesday, April 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
meadowe

Total Points: 0
Total Questions: 97
Total Answers: 97

Location: Laos
Member since Fri, Sep 11, 2020
4 Years ago
;