Reputation: 54123
I have the following javascript:
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
</script>
I have the following in my code behind.
[WebMethod]
public static string sayHello()
{
return "hello";
}
This works and shows hello.
Now, if I do:
[WebMethod]
public static string sayHello(string args)
{
return "hello";
}
Then I get internal server error 500 as a reply.
How can I change this to allow me to send actual data to the server side?
Upvotes: 0
Views: 259
Reputation: 9780
data: {
args: "hello world"
},
Also, remove both contentType
and dataType
as well.
Also, please add the traditional: true
. Your resulting request will be:
$.ajax({
type: "POST",
url: "WebForm3.aspx/sayHello",
traditional: true,
data: {
args: "hello world"
},
success: AjaxSucceeded,
error: AjaxFailed
});
Upvotes: 5
Reputation: 54123
Well,
This works:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "WebForm3.aspx/Test",
data: '{"data":"abc"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
}
});
});
Upvotes: 1