0

I do not know what the problem is, that I can't connect to my Webservice. It says error 404. As far as I can tell it should work. Since I removed the Code from the *.cshtml in to a seperate .js file its not working anymore.

The Javascript file is in a subfolder "Scripts/Reps/report.ui.js"

I'd like to call the method GetReport from The HomeController.cs with the standard configuration.

Can you tell me what I'm doing wrong, or if I totally missunderstood that part? Thank you for helping me

$("#createReport").click(function () {
    var postdata = JSON.stringify(
        {
            "EvaluatedPrice": $("#estimatedPriceValue").text(),
            "Address": $("#address").text(),
            "SomeValue": "Som RandomValue To test"
        });

    try {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("GetReport", "Home")',
            data: postdata,
            dataType: "json",
            success: getSuccess,
            error: getFail
        });
    }
    catch (e) {
        alert(e);
    }
});

function getSuccess(data, textStatus, jqXHR) {
    alert(data.Response);
}

function getFail(jqXHR, textStatus, errorThrown) {
    alert(jqXHR.status);
}
public class HomeController : Controller
    {
[HttpPost]
        [Route("GetReport")]
        public async Task<ActionResult> GetReport(HttpContext context)
        {
            string jsonString = string.Empty;

            var data = context;

            return Json(new { data = 12 });
        }
    }
}

1 Answer 1

3

@Url.Action("GetReport", "Home") it will not work in a *.js file. razor syntax only works in *.cshtml file. yout need to store that url in a variable.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.