1

I have the following script:

$.ajax({
    url: '/Switch/showOptions',
    data: {
        switchid: "331",

    },
    type: 'get',
    success: function (html) {
        $('#showoptions').html(html);
        $("#showoptions").dialog("show"); //This could also be dialog("open") depending on the version of jquery ui.
        OnSuccess('createsuccess')         
    },
});

What I am trying to do is to fire an OnSuccess script after showing the dialog, currently I am getting an exception that OnSuccess is not defined? So can anyone advice how I can fire an OnSuccess script for my Ajax call?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
John John
  • 1
  • 72
  • 238
  • 501
  • WHat do you mean `OnSuccess` - you're already using the `success` callback? – tymeJV May 22 '14 at 23:50
  • what is `OnSuccess`? Is it a function somewhere? where is it defined, can you show the rest of the code? – Luke May 22 '14 at 23:50
  • Is it as simple as creating a function and calling it, was that your question? – von v. May 22 '14 at 23:50
  • Can you show us how you've declared OnSuccess()? – Kong May 22 '14 at 23:53
  • when i use Ajax helpers inside asp.net mvc i have an option to define functions such as Onsuccess , OnComplete,Onfailure , etc. so i am trying to achive the same here.. – John John May 22 '14 at 23:55
  • “I have an option to define functions such as Onsuccess , OnComplete,Onfailure , etc. so i am trying to achieve the same here” Makes no sense. The jQuery `.ajax` you have already has a `success:` callback you can use. So this seems redundant for no reason. – Giacomo1968 May 23 '14 at 00:07
  • the sucess in my case will update the div with the returned html, but i want to fire a script after updating the div with the html, the script will display a confirmation message. – John John May 23 '14 at 00:09

2 Answers2

1

Try this using jQuery.when() as described in this answer here:

Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.

So with that in mind, here is your code reworked to use jQuery.when():

var ajax_action = $.ajax({
    url: '/Switch/showOptions',
    data: {
        switchid: "331",

    },
    type: 'get',
    success: function (html) {
        $('#showoptions').html(html);
        $("#showoptions").dialog("show"); //This could also be dialog("open") depending on the version of jquery ui.
    },
});

$.when(ajax_action).then(function(data, textStatus, jqXHR) { OnSuccess('createsuccess'); });

Or if you just want to test this concept, change the jQuery.when() to be this:

$.when(ajax_action).then(function(data, textStatus, jqXHR) { alert('I am a success!'); });

EDIT: Last edit to try and address original posters request. They want to fire a script called createsuccess, so just do this:

$.when(ajax_action).then(function(data, textStatus, jqXHR) { createsuccess(); });
Community
  • 1
  • 1
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • still i am getting the following exception 0x800a1391 - Microsoft JScript runtime error: 'OnSuccess' is undefined – John John May 23 '14 at 00:01
  • 1
    @johnG If `OnSuccess` is undefined I am not clear what you are trying to do as your action is already happening in the scope of the jQuery `success:` callback. `OnSuccess` seems to be a separate JavaScript function you want to use, so that is what my solution is hinged on. – Giacomo1968 May 23 '14 at 00:05
  • i used to work with ajax.actionlinks and ajax.beginforms , which are ajax helpers used by the asp.net mvc . where inside the ajax.actionlink i can define an onsuccess script that fires when the ajaxcall is successful . so i want to have similar script for my ajax call.. – John John May 23 '14 at 00:08
  • @johnG 100% unclear how the built in Ajax `:success` or my solution doesn’t address that issue. – Giacomo1968 May 23 '14 at 00:10
  • “where inside the ajax.actionlink i can define an on success script that fires when the ajaxcall is successful.” My solution addresses this issue. you need to actually create an `OnSuccess ` function to get this to work. Will edit to make it all clearer. – Giacomo1968 May 23 '14 at 00:14
  • the OnSuccess is nto a function , it means that it will fire a script when the ajax call is success. the script which should fire is "createsuccess " in my case – John John May 23 '14 at 00:16
  • @johnG Do you understand how JavaScript & jQuery works? I have edited my answer to use `createsuccess` as you describe. But I think this is the most I can do to help you. If you find this useful, please remember to up vote my answer & check it off as “answer” if it fully solves the issue. – Giacomo1968 May 23 '14 at 00:20
  • 1
    thanks for the help , basically this worked for my $.when(ajax_action).then(function(data) { createsuccess(data); }); – John John May 23 '14 at 00:24
1

Your code should work if you add a ; after your OnSuccess call. This isn't an event to be fired but it is a a function that will be executed after the first two statements.

function OnSuccess(p_Success) {
    alert('it worked!');
}


$.ajax({
    url: '/Switch/showOptions',
    data: {
        switchid: "331",

    },
    type: 'get',
    success: function (html) {
        $('#showoptions').html(html);
        $("#showoptions").dialog("show"); //This could also be dialog("open") depending on the version of jquery ui.
        OnSuccess('createsuccess');
    },
});
wilsotc
  • 800
  • 4
  • 10
  • 1
    If you read my answer & the comments, the original poster was confused. Instead of `OnSuccess('createsuccess');` They really want the behavior of `createsuccess()`. Confusing but cleared up. – Giacomo1968 May 23 '14 at 00:33