jQuery AJAX POST请求导致错误500:内部服务器错误

时间:2014-07-02 16:54:55

标签: jquery ajax json wcf

我使用以下代码创建了一个支持AJAX的WCF服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace AJAX
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CalculatorService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int GetRandomNumber()
        {
            Random rand = new Random();
            return rand.Next(100);
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int Add(int a, int b)
        {
            return a + b;
        }

        [OperationContract]
        [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
        public int Subtract(int a, int b)
        {
            return a - b;
        }
    }
}

现在,我正在尝试使用以下代码向各种方法发出请求:

function random() {
    $.ajax
    ({
        type: "GET",
        url: "/CalculatorService.svc/GetRandomNumber",
        cache: false,
        dataType: "json",
        success: function (jsonData) {
            var number = jsonData.d;
            alert(number);
        },
        error: function (error) {
            alert("Error!");
        }
    });
};

function add() {
    var inputData = {"a": 10, "b": 5};

    $.ajax
    ({
        type: "POST",
        url: "/CalculatorService.svc/Add",
        cache: false,
        data: JSON.stringify(inputData),
        dataType: "json",
        success: function (jsonData) {
            var number = jsonData.d;
            alert(number);
        },
        error: function (error) {
            alert("Error");
        }
    });
};

function subtract() {
    var inputData = { "a": 10, "b": 5 };

    $.ajax
    ({
        type: "POST",
        url: "/CalculatorService.svc/Subtract",
        cache: false,
        data: JSON.stringify(inputData),
        dataType: "json",
        success: function (jsonData) {
            var number = jsonData.d;
            alert(number);
        },
        error: function (error) {
            alert("Error");
        }
    });
};

$(document).ready(function () {
    $("#randomNumberButton").click(function () {
        random();
    });

    $("#addButton").click(function () {
        add();
    });

    $("#subtractButton").click(function () {
        subtract();
    });
});

现在,由于一些奇怪的原因, GetRandomNumber 请求完美无缺。但是,对于添加减去 POST请求,我在控制台中收到“错误500:内部服务器错误”。我该如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:1)

在每个POST ajax请求中添加以下行后,一切运行良好。

contentType: "application/json; charset=utf-8",
相关问题