简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM