0

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 http://www.mattdailey.net/codesnippetapp/Home/GetCodeData instead it is http://www.mattdailey.net/Home/GetCodeData

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

$.ajax({
            url: '/Home/GetCodeData',
            type: 'Post',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(selectedSnippetID),
            success: function (data) {
                if (data.success) {
                    $("#snippetcode").val(data.snippetCode);                    
                } else {
                    alert('invalid ID' + data.success);
                }
            }
        });
    [HttpPost]
    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?

UPDATE: 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.

mdailey77
  • 1,673
  • 4
  • 26
  • 52

3 Answers3

0

/ 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

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
0

use JsonRequestBehavior.AllowGet

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 },JsonRequestBehavior.AllowGet);
    }
    return Json(new { success = false },JsonRequestBehavior.AllowGet);

}
shady youssery
  • 430
  • 2
  • 17
  • You do not need to specify `JsonRequestBehaviour.AllowGet` for an **HttpPost** action/call (which the OP is doing). It is needed only if you are returning some json from an GET action method. – Shyju Aug 03 '16 at 20:14
0

This should be a GET operation as you are trying to return Json to the client.

$.ajax({
            url: '/Home/GetCodeData',
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: JSON.stringify(selectedSnippetID),
            success: function (data) {
                if (data.success) {
                    $("#snippetcode").val(data.snippetCode);                    
                } 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 }, JsonRequestBehavior.AllowGet);
        }
        return Json(new { success = false }, JsonRequestBehavior.AllowGet);

    }
Gary
  • 199
  • 5
  • 12