I'm getting an error because when i put /
at the end of the URL the angular 2 service is looking for the current URL plus the MVC controller
URL,
For example:
Without /
: localhost/controller/action
With /
: localhost/controller/controller/action
this is my code:
MVC Controller:
[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 service:
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 :)