mdailey77 mdailey77 - 1 year ago 71
JSON Question

jsonresult is returning html not json

I published an ASP.NET MVC application to Godaddy. I'm having an issue with an Ajax call that is supposed to return a JSON object it returns the HTML of my site's index page. I previously had a problem with the app's menu bar links, where they were redirecting to my site's main page. I was able to solve the problem by adding a rule to my site's web.config that excluded the subfolder containing the app:

<add input="{REQUEST_URI}" pattern="^/(codesnippetapp)" negate="true" />
I checked the dev console in Chrome and the request URL is wrong. The URL should be instead it is

Here is the Ajax call and the JsonResult functions that retrieve the JSON:

url: '/Home/GetCodeData',
type: 'Post',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(selectedSnippetID),
success: function (data) {
if (data.success) {
} else {
alert('invalid ID' + data.success);
public JsonResult GetCodeData(int snippetID)

CodeSnippet returnedsnippet = db.CodeSnippets.FirstOrDefault(d => d.Id == snippetID);
if (returnedsnippet != null)
return Json(new { success = true, snippetCode = returnedsnippet.SnippetCode });
return Json(new { success = false });


What do I need to add to my app's web.config? Or do I need to add code to my site's web.config?

I tried using the GET method but got an internal server error. I moved the script from an external file into the View itself by using the razor @section code like this:

@section Scripts
... jQuery code

Then added this to the _Layout.cshtml:

@RenderSection("Scripts", required: false)

I did add the razor @Url.Action helper to the Ajax url. I also changed the way I publish the application to Godaddy, which I think helped too. I changed from the FTP method to Filesystem. I then uploaded the files manually via FTP. It's now working.

Thanks everyone for the help. I wrote out my steps hoping this will help someone else in a similar situation.

Answer Source

/ at the beginning of the url value will make it as the root of your site (not your app under that).

Use Url.Action helper method to generate the path to your action method.

url: '@Url.Action("GetCodeData","Home")',

This should work if your javascript is inside a razor view. If your code is inside an external js file, call this method in a razor view and assign it to a variable and use it in your js file as explained in the second part of this post