【问题标题】:Call MVC Controller from AngularJS in ASP.Net MVC with web API controller使用 Web API 控制器在 ASP.Net MVC 中从 AngularJS 调用 MVC 控制器
【发布时间】:2018-06-18 04:42:12
【问题描述】:

我正在开发一个使用 ASP.NET MVC、AngularJS 和 WebAPI 2.0 的项目。我正在从 AngularJS 调用一个 mvc 控制器。从 AngularJS 调用 MVC 控制器时出现错误。

型号

public class HotelPayment
{
    public string TransactionID { get; set; }
    public string Referencenumber { get; set; }
    public string destination { get; set; }
    public string hotelname { get; set; }
    public string hoteladdress { get; set; }
    public string board { get; set; }
    public string noofrooms { get; set; }
    public string checkin { get; set; }
    public string checkout { get; set; }
}

MVC 控制器

[HttpPost]
public string Sendpayment(HotelPayment objData)
{
   Session["products"] = objData;
   return objData.url;
}

从角度控制器调用 MVC 方法

$http({
    method: "POST",
    data: paymentData,
    contentType: "application/json; charset=utf-8",
    url: "/Home/Sendpayment"
});

我收到此错误。

为什么我会看到这个?我的 MVC 控制器和 API 是同一个项目中的控制器。这个错误有什么关系吗?

【问题讨论】:

  • 你有没有试过在方法Sendpayment调试,http状态码500表示服务器在处理请求时遇到了错误。
  • Server 端有问题,我们猜不到,捕获异常并记录详细信息以获取更多信息,使用try-catch
  • 在 localhost 中调试时,它工作正常。但是在冲洗云之后,我得到了这个错误。
  • 首先检查服务,然后尝试邮递员进行测试服务。
  • 您能否在此处发布您的 HotelPayment 对象以了解更多信息

标签: asp.net angularjs asp.net-mvc asp.net-mvc-4


【解决方案1】:

我使用启用跨域请求 (CORS) 解决了这个错误。

Enabling Cross-Origin Requests

【讨论】:

    猜你喜欢
    • 2020-04-08
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多