我收到错误,因为当我将/
放在网址末尾时,角度2服务正在寻找当前网址以及MVC controller
网址
例如:
没有/
:localhost/controller/action
使用/
:localhost/controller/controller/action
这是我的代码:
MVC控制器:
[HttpGet]
public async Task<ActionResult> GetItemNames()
{
var response = await _client.GetAsync("invoiceitems");
var items = new List<Item>();
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
return Json(new { success = false, errors = new string[]{"Please refresh the page or login."} }, JsonRequestBehavior.AllowGet);
}
if (response.IsSuccessStatusCode) {
var result = response.Content.ReadAsStringAsync().Result;
items = JsonConvert.DeserializeObject<List<Item>>(result);
}
var itemNames = items.Select(i => new {
code = i.plCode,
name = i.name,
searchText = i.name.ToLower()
}).ToList();
return Json(new { success = true, data = itemNames }, JsonRequestBehavior.AllowGet);
}
}
Angular 2服务:
private invoiceUrl = 'invoice/';
getItemNames(): Observable<any> {
const itemsUrl = this.invoiceUrl + 'getitemnames';
return this.http.get(itemsUrl)
.map((res: Response) => res.json());
// TODO: Catch errors.
}
TIA:)