0

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.

3
  • 1
    What is the problem? What is the error? Commented Jul 1, 2014 at 17:03
  • Can you show your HTML as well? Commented Jul 1, 2014 at 17:03
  • the error is that not recognise the function "inicioConsultar()", but if i delete the ajax call, everything it is ok!!! Commented Jul 1, 2014 at 17:09

1 Answer 1

2

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.

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

5 Comments

Or, consequently "@Url.Action('ObtenerCapas')"
but before this code was in my index and it doesnt have problem!
That's unlikely. That has never been valid JavaScript. Is your Index file preprocessed in some way?
i tried this but it doesnt work, the error it is the same "Error: ReferenceError: 'inicioConsultar' is undifened, function Consultar"
well, i tried onther time because in the same file i have more ajax call and this is the problem!!! Thanks very much, and sorry for my english

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.