0

Hi Below is the code in which through ajax call in jquery i AM calling function 1:"setEmailAFriendCount" in which we are sending variable having json datatype but I want to call the same function from ajax call but need to send extra string parameter to it.

I am confused, how to send one. P

function getEmailAFriendCountDynamic(ArticleURL, Id) {
        alert('Hi');
        var location = ArticleURL;
        if (location.indexOf('?') >= 0) {
            location = location.substring(0, location.indexOf('?'));
        }

        $.ajax({
            url: 'http://contactimporter.mercola.com/EmailArticleCount.aspx?url=' + location,
            dataType: "jsonp",
            jsonpCallback: "setEmailAFriendCount"
        });
    }
Function 1:

function setEmailAFriendCount(json) {
        $('#MyTextbox').text(json.count);
    }
Function 2:
    function setEmailAFriendCount(json,emailtofrdclientid) {
        $('#' + emailtofrdclientid + '').text(" : " + json.count);
    }
TRR
  • 1,637
  • 2
  • 26
  • 43
  • 1
    what is `emailtofrdclientid`? Are you trying to send that too from server? –  May 28 '12 at 06:26
  • send it using `data:` attribute – TRR May 28 '12 at 06:28
  • do you mean that you want to send two arguments to `setEmailAFriendCount` instead of just the json ? – wong2 May 28 '12 at 06:30
  • If you're asking about overloading methods in javascript, all JS parameters are essentially optional, so if you define setEmailAFriendCount as a 2-parameter function, you can still call it with just one parameter - the second will just appear to be undefined. Whether it's a good idea is another question. See this post for more ideas: http://stackoverflow.com/questions/1529077/handling-optional-parameters-in-javascript – Simon MᶜKenzie May 28 '12 at 06:42

2 Answers2

0

the callback params are created in the server-side. if you want to return more then one param return a json object with as many params as you need. the return should get that object and parse it correctly. in order to send a multi params to the server send a json object and pass it under the data property.

$.ajax({
  type: "[GET/POST]",
  url: "[your url]",
  data: { name: "John", location: "Boston" }, //your object here
  success: function(data) {
    //parse your json return data here
  }
});

more info about jQuery ajax here.

Sagiv Ofek
  • 25,190
  • 8
  • 60
  • 55
0
function getEmailAFriendCountDynamic(ArticleURL, Id) {
        alert('Hi');
        var location = ArticleURL,
            self = this;
        if (location.indexOf('?') >= 0) {
            location = location.substring(0, location.indexOf('?'));
        }

        $.ajax({
            url: 'http://contactimporter.mercola.com/EmailArticleCount.aspx?url=' + location,
            dataType: "jsonp",
            jsonpCallback: "setEmailAFriendCount",
            success: function(jData) {
                 self.setEmailAFriendCount(jData, Id);
              }
        });
    }

Function 1:

function setEmailAFriendCount(json) {
        $('#MyTextbox').text(json.count);
    }
Function 2:
    function setEmailAFriendCount(json,emailtofrdclientid) {
        $('#' + emailtofrdclientid + '').text(" : " + json.count);
    }

I think this code is what you looking for...

efirat
  • 3,679
  • 2
  • 39
  • 43