简体   繁体   English

jQuery $ ajax函数在C#中返回500 Internal Server Error

[英]Jquery $ajax funtion returns 500 Internal Server Error in C#

when clicking a button I am passing some data to .cs file after page load.But I got 500 Internal Server Error while calling ajax function. 当单击按钮时,页面加载后我正在将一些数据传递到.cs文件。但是调用ajax函数时出现500 Internal Server Error

Ajax Function, Ajax功能

              $.ajax({
                type: "POST",
                url: "Home.aspx/getSelectedData",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                //async: true,

calling Function, 调用函数,

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public List<pageResult> getSelectedData(string search_value)
{}

I got the following error, 我收到以下错误,

 **POST http://localhost:4519/Home.aspx/getSelectedData 500 (Internal Server Error)**

Use the .cs file like this, 像这样使用.cs文件,

[WebMethod]
public static List<pageResult> getSelectedData(string search_value)
{}

For ajax calling in aspx you should define the method as Static then only it will working for you. 对于用aspx调用的Ajax,您应该将方法定义为Static,然后只有它对您有用。

contentType is the type of data you're sending, so application/json ; contentType是您要发送的数据类型,因此application/json ;

The default is application/x-www-form-urlencoded; charset=UTF-8 默认值为application/x-www-form-urlencoded; charset=UTF-8 application/x-www-form-urlencoded; charset=UTF-8 . application/x-www-form-urlencoded; charset=UTF-8

If you use application/json , you have to use JSON.stringify() in order to send JSON object. 如果使用application/json ,则必须使用JSON.stringify()来发送JSON对象。

JSON.stringify() turns a javascript object to json text and stores it in a string. JSON.stringify()将JavaScript对象转换为json文本并将其存储在字符串中。

$.ajax({
      type: "POST",
      url: "Home.aspx/getSelectedData",
      data: JSON.stringify(data),
      contentType: "application/json; charset=utf-8",
      dataType: "json",

change your ajax type to get 更改您的ajax类型以获取

$.ajax({
                type: "get",
                url: "Home.aspx/getSelectedData",
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM