tags:

views:

49

answers:

3

Hi,

The following is the code snippet what Im trying with ASP.NET MVC with ajx call to Action.

But action is not calling. Please help me?

$.ajax({
    type: "POST",
    url: "/Sample/TestSubmit",
    data: $.toJSON(result),
    contentType: "application/json;charset=utf-8",
    dataType: "json",
      success: function(msg) {
        var succeed = msg == 'OK';
        if (!succeed) {
            alert(msg);
        } else {
            alert('Saved');
        }
    },
    error: function(){
    alert('failed to save');
    }
});
A: 
  1. Are you sure you are loading the jquery library?

  2. did you have it inside a global function launch or launched as part of an event?

    $(document).ready(function(){
    $.ajax({
        type: "POST",
        url: "/Sample/TestSubmit",
        data: $.toJSON(result),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
          success: function(msg) {
            var succeed = msg == 'OK';
            if (!succeed) {
                alert(msg);
            } else {
                alert('Saved');
            }
        },
        error: function(){
        alert('failed to save');
        }
    });
    

    });

pixeline
i think 'TestSubmit' is more likely to be in a click even than immediately on DOMReady. Regardless, the method pasted doesn't rely on any DOM nodes
David Hedlund
Its in a function like function save(){'''''''//code which I gave you before}
Yaswanth
As the code is just for testing, I gave name as TestSubmit. Eventhough, what ever may be the name inside controlle(except C# keywords) it has to work.
Yaswanth
yes, this definitely doesn't have to be wrapped in the DOMReady event; you want to address Dimitrov's comment to your question, that's really the first step for debuggning this. you might also want to throw in an alert into your code to see that `save` is being called at all
David Hedlund
it needs to be *at least* wrapped in the DOMReady event, but most of the time it will be on a submit/click event attached to a DOM form or input element.Yaswanth, i don't see any save() function in your code sample. Please provide the whole code, i am no mind reader.
pixeline
@pixeline: the code does not contain any single reference to any DOM nodes, it does not require the DOM to be available to be able to execute.@Yaswanth: you probably want to elaborate on how you're populating `result` that you are passing as data. it might be something in there that is breaking the code, and stopping the execution
David Hedlund
@David: no, but it requires jquery to be available. Hence the waiting for the DOMReady, at the very least. Simply launching the function right after loading the library is hazardous and exposing the code to failure due to browser discrepancies.
pixeline
@pixeline: what? `$(document).ready` *is* jQuery code. jQuery *will* be available immediately after including the file, otherwise even that wouldn't work. a javascript include will halt all further javascript execution until that file has been completely downloaded and executed. the only way to deviate from that would be to manually download the file, (through ajax, say), and eval it (which is not very uncommon, for performance reasons), but again, loading the jQuery library that way would break `$(document).ready` as well
David Hedlund
@David: indeed, my bad. Still, i wonder if this question is at all solvable with so little information.
pixeline
A: 

sorry friends. the following is the full code

function save() { function TestData(code,text) { this.Qcode = code; this.Qtext = text; } function TestDataArray(testArray) { this.TestArray = testArray; } var testArr = new Array();

$('#questions tr').each(function(row, rowItem) {

testArr[testArr.length]= new TestData('Qcode',$(rowItem.cells[1]).text());

});

var result = new TestDataArray(testArr);

$.ajax({

    type: "POST",

    url: "/Sample/TestSubmit",

    data: ""+$.toJSON(result),

    contentType: "application/json;charset=utf-8",

    dataType: "json",

      success: function(msg) {

        var succeed = msg == 'OK';

        if (!succeed) {
            alert(msg);
        } else {
            alert('Saved');
        }
    },
    error: function(){
    alert('failed to save');
    }
});

}

. And my problem was controller is not calling mean my action TestSubmit.

This is because I have to add jquery.json-2.2.js file to my aspx page to get toJSON() work. And now controller is calling action .

Yaswanth
A: 

We really don't have a lot of info here. There's no immediate syntax error in the part of the code you've shown, so there's nothing for us to point out unless you provide us some more info.

My bet is that the error is one of the following, but from what we've got, i cannot judge their relative plausibility:

  • save is never being executed. incorrect selector binding an event, perhaps, or the binding being executed before whatever is being hooked up is available in the DOM?
  • the code that populates response has some error, causing the execution of the save function to halt
  • response in $.toJSON(response) is undefined
David Hedlund
yeah exactly the 3rd reason what you thought. Its because i didnt add jquery.json-2.2.js in my aspx page to refer toJSON() function. And sorry , I'm new to these type of forums that's why didn't add details of my code.
Yaswanth
no worries, glad you sorted it out in the end
David Hedlund

related questions