简体   繁体   English

从C#WebMethod返回到JavaScript AJAX调用的内部服务器错误

[英]Internal Server Error returning from C# WebMethod to JavaScript AJAX call

I can't get the AJAX call to get the return from the C# WebMethod. 我无法获得AJAX调用来从C#WebMethod获得收益。 It always returns the AJAX error "Internal Server Error". 它总是返回AJAX错误“内部服务器错误”。

A button calls JS function: 一个按钮调用JS函数:

<button id="btn" onclick="Create();">foo</button>

The JS function: JS函数:

function Create() {
var data = {
    value1: 'string 1',
    value2: 'string 2',
    value3: 'string 3'
};
$.ajax({
    url: 'default.aspx/Create',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ formData: data }),
    async: true,
    success: function (msg, status) {
        alert("success " + msg.d);
    },
    failure: function (data) {
        alert("failure " + msg.d);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus + " : " + errorThrown);
    }
  });
return false;
}

And the C# WebMethod: 和C#WebMethod:

[WebMethod]
public static string Create(string data)
{
    return "webmethod string";
}

Can anyone point me where is the mistake? 谁能指出我的错在哪里?

You are returning a string but the success method is expecting json. 您正在返回一个字符串,但是成功方法需要json。 Ajax doc here: http://api.jquery.com/jquery.ajax/ 此处的Ajax文档: http : //api.jquery.com/jquery.ajax/

Datatype property: 数据类型属性:

dataType (default: Intelligent Guess (xml, json, script, or html)) Type: String The type of data that you're expecting back from the server. dataType(默认值:Intelligent Guess(xml,json,脚本或html))类型:String期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). 如果未指定,则jQuery将尝试根据响应的MIME类型来推断它(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4中,脚本将执行该脚本,而其他所有内容将是以字符串形式返回)。 The available types (and the result passed as the first argument to your success callback) 可用的类型(并将结果作为第一个参数传递给您的成功回调)

Change datatype to "text" 将数据类型更改为“文本”

Also chnage your paramater from "formData" to "data" 还将参数从“ formData”更改为“ data”

code: 码:

$.ajax({
    url: 'default.aspx/Create',
    type: 'POST',
    dataType: 'text',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({ data: data }),
    success: function (data) {
        alert("success " + data);
    },
    failure: function (data) {
        alert("failure " + msg.d);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(textStatus + " : " + errorThrown);
    }
  });

3:d edit here: you are sending an object in you ajax call but your paramater on server side is a string. 3:d在此处编辑:您正在ajax调用中发送对象,但是服务器端的参数是字符串。 change it to a class instance with the same name properties as the object that you are sending 将其更改为具有与要发送的对象相同名称属性的类实例

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

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