views:

28

answers:

1

aspx site:

<script type="text/javascript">
function AjaxTest() {

  var codeVal = "hello world";

  if (codeVal) {
    $.ajax({
             type: "POST",
             url: "CheckAge",    
             data: { code: codeVal },
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             async: true,
             cache: false,
             success: function (result) {
                        alert("in ajax success");
             },
             error: function () {
                        alert("error");
             }

          });
  } 
}

Its double checked that the javascript function is called.

Controller:

[HttpPost]
public JsonResult CheckAge(String code)
{
      return Json("abc");
}

It ended up always in the ajax - error - function. The Controller function isnt called anyway. Why? Why get I always an error? What is wrong?

+2  A: 

Check your url that you are posting to. It seems that you are missing the controller part. E.g. it should read /{controller}/{action}.

If that script is directly in the view (i.e. not in an external javascript file) you could have something like:

$.ajax({
    type: "POST",
    url: <%= Url.Action("CheckAge", "ControllerName") %>,    
    data: { code: codeVal },
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    cache: false,
    success: function (result) {
        alert("in ajax success");
    },
    error: function () {
        alert("error");
    }
});

Also, I find it advantageous to use firebug to debug ajax stuff. You can set break points in your javascript and also see all the requests and responses.

HTHs,
Charles

EDIT: Try simplifying things... e.g.

$.post('<%= Url.Action("CheckAge", "ControllerName") %>',
       { code: codeVal },
       function (data) {
           alert("in ajax success");
       },
       "json");
Charlino
Thx 4 the very fast help! Yes, the script is directly in the view. I changed the "url: ... " line exactly as shown above. Result: No more error - but also no other reaction!! The Controller function "public JsonResult CheckAge(String code)" is also NOT called. Do I need a "special" AJAX-Version; I use <script src="/Scripts/Ajax_0911/Start.js" type="text/javascript"></script> Is that ok?
**Exactly** as shown above? You did change `"ControllerName"` to your controller name... right? Have you used firebug to see what's going on? Sorry, not following you in regards to a "special" AJAX-Version... also not sure what that whole `Scripts/Ajax_0911/Start.js` javascript file is.
Charlino
**Excatly** means of course that I replaced "ControllerName" with the name of my controller, put two single quotes around "<%= ... %>" and changed "<%=" to "<%:" ;-) No, will set up firebug tomorrow (its already late here ;-) "Ajax Version" should mean: Somewhere in YOUR code you will find/worte something like **<script src=".../MicrosoftMvcAjax.js" type="text/javascript"></script>**, or? But what do you excatly use/wrote? **"MicrosoftMvcAjax.js"** or **"MicrosoftAjax.js"** or ???
See my edit and try that. I suspect it might have something to do with `contentType: "application/json; charset=utf-8"`. Just a hunch. Also, what is the value of codeVal? The code that you are trying to run simply requires the jQuery library to be loaded.
Charlino
I installed firebug, activated scripts in firebug, then I simplified like your Edit. "codeVal" is hardcoded set to string "test". Firebug shows me the right u r l "/MyController/CheckAge" and as data my hardcoded string "hello". But i didnt get any callback. The set Breakpoint in MyController.cs isnt reached. But no error pops up. Strange isnt it?!
Have to say thank you and good night - 18 hours are enaugh for me. Good night - will continue tomorrow...