I seem to be encountering an issue with retrieving data using ajax. When I test locally, it returns the desired output. However, upon publishing the project on the server with IIS, it shows a HTML Code of my page along with an error message "syntax Error: Unxpected token < in JSON as position 0". Any assistance would be greatly appreciated. Thank you :)
Below is the code snippet:
Controller:
[HttpGet]
public ActionResult getregion(string zname)
{
SelectList list = RegionList(zname);
var serializedData = JsonConvert.SerializeObject(list, Formatting.Indented,
new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});
return Json(list, JsonRequestBehavior.AllowGet);
}
The above code is designed to fetch a list of regions in JSON format.
public SelectList RegionList(string zname)
{
CustomerModel cust = new CustomerModel();
List<SelectListItem> regionname = new List<SelectListItem>();
try
{
dt1.Clear();
MySqlConnection mycon = new MySqlConnection(conn.getconnections("mysql"));
mycon.Open();
if (zname == null || zname == "")
{
cmdsql = mycon.CreateCommand(); cmdsql.CommandText = "SELECT regionname FROM tbl_region WHERE STATUS=1 group by regionname order by regionname asc";
}
else
{
cmdsql = mycon.CreateCommand(); cmdsql.CommandText = string.Format("SELECT regionname FROM tbl_region WHERE STATUS=1 and zonecode='{0}' group by regionname order by regionname asc", getzonecode(zname));
}
drsql = cmdsql.ExecuteReader();
try
{
if (drsql.HasRows)
{
regionname.Add(new SelectListItem { Text = "- Select -", Value = "", Selected = true });
regionname.Add(new SelectListItem { Text = "All Region", Value = "AllRegion" });
while (drsql.Read())
{
regionname.Add(new SelectListItem { Text = drsql["regionname"].ToString().ToUpper().Trim(), Value = drsql["regionname"].ToString().ToUpper().Trim() });
}
}
}
catch (Exception ex) { cust.WriteToFile(ex.ToString()); return null; }
}
catch (Exception ex) { cust.WriteToFile(ex.ToString()); }
return new SelectList(regionname, "Value", "Text");
}
This method is utilized for fetching the list of regions.
JAVASCRIPT:
$(function () {
$('#zoneitem').change(function () {
var item = $('#zoneitem').val();
var region = $("#regionitem");
region.empty();
if (item != "") {
$("#reportsitem").val($("#reportsitem option:first").val());
}
$.ajax({
type: "GET",
url: "Home/getregion",
contentType: "application/json; charset=utf-8",
data: { zname: item },
cache: false,
dataType: "json",
success: function (result) {
for (var i = 0; i < result.length; i++) {
region.append('<option value="' + result[i].Value.trim() + '">' + result[i].Text.trim() + '</option>');
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});
This pertains to my _reference.js code, where debugging indicates that the returned result is in HTML format of my webpage. Kindly assist me in resolving this issue. Despite researching and attempting various solutions, the problem persists.