Reputation: 139
I have an ASP.NET MVC application. I am trying to call a WebAPI controller. The controller works fine when I call it via $.get in jQuery. After finishing the callback function everything should stop and I should be able to see my web page. However after finishing the callback function the browser is taking me to the raw JSON view page in chrome. In IE it asks me if I want to download returned .json file. Why is this happening. Here is my code:
function onNLookupSearch() {
var uri = window.location.protocol + "//" + window.location.host;
var searchTerm = $("#nlookupsearch").val();
var resultUrl = window.location.href = uri + "/api/v1/GetNetworkName?networkName=" + searchTerm;
$.get(resultUrl, function (data1) {
localData1 = data1;
$("#sdnetworkselect").kendoDropDownList({
dataSource: localData1,
dataTextField: "NETWORK_NAME",
dataValueField: "NETWORK_ID"
});
$("#hdnetworkselect").kendoDropDownList({
dataSource: localData1,
dataTextField: "NETWORK_NAME",
dataValueField: "NETWORK_ID"
});
});
}
Here is my WebAPI controller code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using CPT2.Data;
namespace CPT2.Controllers
{
public class GetNetworkNameController : ApiController
{
private CMDBdbContext _ctx;
public GetNetworkNameController(CPT2.Data.CMDBdbContext ctx)
{
_ctx = ctx;
}
public IQueryable<CPT2.Data.network_sourceid_map> Get([FromUri] string networkName)
{
var result =
_ctx.network_sourceid_map.Where(
nw => nw.NETWORK_NAME.Contains(networkName) || nw.SOURCE_NAME.Contains(networkName));
var myList = result.Select(item => item.NETWORK_NAME + " - " + item.SOURCE_NAME + " [" + item.SOURCE_ID + "]").ToList();
//return myList;
return result;
}
}
}
Lets assume I search for "ESPN". That's the search term in the code above.
After this function is done I should stay on page however I am redirected to a raw view of the returned data by Chrome and IE asks me to save a JSON file.
Upvotes: 0
Views: 175
Reputation: 1242
I think your problem is this line:
var resultUrl = window.location.href = uri + "/api/v1/GetNetworkName?networkName=" + searchTerm;
You are setting the window.location.href which is redirecting the entire page to the API "page", which is why you're getting a .json file.
Upvotes: 1