I have following scripts:
function PostChartValues(meter_id, range_type_id, start_date, end_date) {
$.ajax({
url: '@Url.Action("GetMeterReadingsTimeSeries", "Widget")',
type: 'POST',
data: { MeterType: meter_id, DateRangeType: range_type_id, StartDate: start_date, EndDate: end_date },
beforeSend: function () {
$("#chart_loading_div").show();
},
complete: function () {
$("#chart_loading_div").fadeOut();
},
success: function (result) {
$("#chart").html(result);
},
error: function (result) {
alert("Seçilen kritere uygun veri bulunamadı!");
}
}); //end ajax
} //end PostChartValues
code is working. But When I receive an external .js file above code, I get the error that I wrote in the title.
Why do I get this?
You have asp.net code in there, asp.net-mvc doesn't parse external js files so you'll actuall be trying to make a request to @Url.Action("GetMeterReadingsTimeSeries", "Widget")
which is not a valid url. You'll have to manually write the results of Url.Action("GetMeterReadingsTimeSeries", "Widget")
in you js file.
Musa is absolutely right, but to add to that:
One way to get around that is to provide your rendered variables in an embedded script within your HTML (or view) like so:
<!-- in your view -->
<script type="text/javascript">
var AJAX_URL = '@Url.Action("GetMeterReadingsTimeSeries", "Widget")';
</script>
Then, in your external JavaScript file:
function PostChartValues(meter_id, range_type_id, start_date, end_date) {
$.ajax({
url: AJAX_URL,
// ..
});
}
As a way to try to avoid overlapping variable names, I use all-caps.