0

I am having some issues with Passing Parameters in a jQuery ajax call to an ASP.NET webmethod and it is BUGGING me all day long.

I've seen the solution of how it is being done and yet it is not working for me.

Here is some code:

function GetString() {

    var name = "yan";
    var fam = "key";
    alert(name + " " + fam);

    $.ajax
({
    type: "GET",
    url: "'Services/MyService.asmx/returnString",
    dataType: "json",
    //data: "{ 'fname' : '" + name + "' , 'lname' : '" + fam + "'}",
    //data: "{ 'fname' : 'name' , 'lname' : 'fam'}",
    //data: '{"fname":"Chris","lname":"Brandsma"}',
    data: "{'fname':'Chris','lname':'Brandsma'}",
    contentType: "application/json; charset=utf-8",
    error: function (jqXHR, textStatus, errorThrown)   //what to do if fails
    {
        alert('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
    },
    success: function (data)           //what to do if succedded
    {
        alert(data.d);
    }
});

}

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string returnString(string fname, string lname)
{
    return ("my name is " + fname + " " + lname);
}

and that's the error I get: Error: responseText:

You can see that I've tried several ways of accomplishing it, none worked. Can anybody point the mistake/ suggest another solution???

thanks in advance

5
  • Is webmethod on an aspx page or webservice, because aspx's webmethod does not support GET. Commented Jan 27, 2013 at 16:44
  • try data: {firstName:"Chris",lastName:"Brandsma"}instead of data: "{'firstName':'Chris','lastName':'Brandsma'}" Commented Jan 27, 2013 at 16:44
  • I have created a webservice and the C# code is written in that file. so I guess its not an aspx but a webservice... Commented Jan 27, 2013 at 16:54
  • @mamdouh - it still doesn't work and fires the error: alert("bad") :( Commented Jan 27, 2013 at 16:55
  • Seriously, Hadn't Anybody encountered such issue ??? Commented Jan 28, 2013 at 8:55

2 Answers 2

3

After I've been trying in the last 24 hours, I've finally succeded :) Here's how it goes:

Code Behind:

[WebMethod]
[ScriptMethod(UseHttpGet=false)]
public string returnString2(string fname, string lname)
{
    return "{ \"FirstName\" : "\" + fname + "\" , \"LastName\" : "\" + lname + "\"}";
}

Notice that the (UseHttpGet=false)

Jquery:

function GetString2() {

    var name = "yan";
    var fam = "key";
    alert(name + " " + fam);

    $.ajax
({
    type: "POST",
    url: 'Services/MyService.asmx/returnString2',
    dataType: "json",
    data: JSON.stringify({ fname: "yan" , lname: "key" }),
    contentType: "application/json; charset=utf-8",
    error: function (jqXHR, textStatus, errorThrown)   //what to do if fails
    {
        alert('bad, ' + errorThrown + ", " + jqXHR.responseText + ", " + textStatus);
    },
    success: function (data)           //what to do if succedded
    {
        alert(data.d);
        var person = $.parseJSON(data.d);
        alert(person.FirstName);
    }
});

}

Notice how type:"Post" and that data line are changed

Thanks everyone who tried to help, all of you contributed a little bit :)

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

Comments

1

In your code change

    type: "GET",

to

    type: "POST",

2 Comments

I already gave it a try.. In addition, in my code behind I have [ScriptMethod(UseHttpGet = true)] doesn't that mean that I should've used type: "GET" in the first place?!?! Anyway, I've tried another suggestion, which didn't comply as well: data: JSON.stringify({ fname: "yan" , lname: "key" }),
When you have no parameters, "GET" works. When you have a parameter, "POST" work but "GET" does not. Please, explain why.

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.