简体   繁体   English

在文件.js中调用ajax

[英]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. 我放入Index.cshtml“ inicioConsultar()”,而ajax出现问题,因为如果删除调用ajax,一切正常。

In loyout, i load jquery and the index it is inside layout. 在loyout中,我加载jquery及其位于布局内部的索引。

Sorry for my english. 对不起我的英语不好。

This is a syntax error: 这是语法错误:

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

That isn't how strings work in JavaScript. 这不是字符串在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. 但是,除非@Url.Action(...)是服务器上的真实URL,或者您的AJAX集具有某种能力可以将该字符串作为函数调用求值,否则,这不会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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