sebaMelgar
sebaMelgar

Reputation: 97

Call ajax in file .js

i have a problem with this :

function inicioConsultar(){
$(function(){
    $('#serviciosU').change(function(){
        if ($('#serviciosU').val()!= "-1")
        {
            $.ajax({
                url: "@Url.Action("ObtenerCapas")",
                data: {urlServicioUsuario:$("#serviciosU :selected").val()},
                dataType: "json",
                type: "POST",
                error: function() {
                    alert("An error occurred.");
                },
                success: function(data) {
                    var items = "";
                    $.each(data, function(i, item) {
                        items += "<option value=\"" + item.Value + "\">" + item.Text +     "</option>";
                    });
                    $("#capas").html(items);
                }
            });
        }
    })
});

I put in my Index.cshtml "inicioConsultar()" and there is a problem with ajax, because if i delete the call ajax everything it is ok.

In loyout, i load jquery and the index it is inside layout.

Sorry for my english.

Upvotes: 0

Views: 56

Answers (1)

user229044
user229044

Reputation: 239291

This is a syntax error:

"@Url.Action("ObtenerCapas")",

That isn't how strings work in JavaScript. You need to escape the inner double quotes, as they're terminating the string.

Try

"@Url.Action(\"ObtenerCapas\")",

However, that wont' solve your problem, unless @Url.Action(...) is a real URL on your server, or your AJAX set has some kind of ability to evaluate that string as a function call.

Upvotes: 2

Related Questions