Ajax - .cshtml调用时出错

时间:2016-08-19 16:06:19

标签: c# ajax response

我有下一个问题: 我正在创建一个.jshtml文件的ajax调用...在localhost中工作正常,但是当在服务器上传时它会抛出一个错误" Forbidden(403)"

这是我的ajax电话:

$.ajax({
    type: "GET",
    url: "~/MyProcessPage.cshtml",
    contentType: "application/x-www-form-urlencoded",
    data: { id: 1 },
    crossDomain: true,
    success: function (res) {
        console.log(res);
    },
    error: function (res) { console.log(res); },
    dataType: 'json'
});

这是我的C#代码:

if (HttpContext.Current.Request.HttpMethod == "GET")
{
    var id= Convert.ToInt32(Request.QueryString["id"]);
    Response.Write(id);
}      

请注意我使用剃须刀但不使用MVC!

谢谢!

1 个答案:

答案 0 :(得分:1)

也许你会改变网址

$.ajax({
    type: "GET",
    url: '@Url.Content("~/MyProcessPage.cshtml")',
    contentType: "application/x-www-form-urlencoded",
    data: { id: 1 },
    crossDomain: true,
    success: function (res) {
        console.log(res);
    },
    error: function (res) { console.log(res); },
    dataType: 'json'
});