0

I have some problem ajax with query string, I can send the data if page does not have and Info.aspx/Save works great. But when I fill somethings with query string then post same data it will return http 500 error. I have nothing with querystring in javascript I use it in C# for id.

 var data = '{name: "' + $("input[name$='name']").val() +
                    '",description: "' + $("input[name$='description']").val() +
                    '",code: "' + $("input[name$='code']").val() +'"}';
$.ajax({
                type: "POST",
                url: "Info.aspx/Save",
                data: data,
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    var result = data.d
                    console.log(result)
                    if (result > 0)
                        success.show();
                    else
                        error.show();
                    Metronic.scrollTo(error, -200);

                },
                error: function () {
                    console.log('err')
                }
            });

    [WebMethod]
    public static int Save(string name, string description, string code)
    {
        ClassInfo classInfo = new ClassInfo();
        return ClassInfo.Save(name, code, description, FileInfo.id);
    }

I just use querystring in C# for filling inputs. Altough I send same data it works without querystring inpgae's adressbar, If it has querystring in addressbar ajax returns me http500 error and Save WebMethod does not work.

3
  • The question is incomplete, how are u sending data?? If say u are trying to send as name value pair, it has to be a json object, it can't be the same as when you call with URL. Commented Sep 22, 2014 at 8:48
  • I added something, these are what you mean? Commented Sep 22, 2014 at 8:53
  • @user4065706 - use JSON.stringify(data); Commented Sep 22, 2014 at 9:09

1 Answer 1

1

data types must be object, no string.

var data = {name: $("input[name$='name']").val() ,description:$("input[name$='description']").val(),code:$("input[name$='code']").val()};

Remove the contentType or change it to

"application/x-www-form-urlencoded; charset=utf-8"

Then

$.post('/Info.aspx/Save',data , 
   function (data) {   
});

Or

$.ajax({
    url: '/Info.aspx/Save',
    data: data 
});

Server side Your json parameter names must be same with the c# paramter names.

[WebMethod(true)]
public static string Save(string name, string description, string code)
{
    return name+" "+description+" "+code;
}

Usable for Web Method and Query String(working for both)
The query string parameters need to be passed as JSON.

data: JSON.stringify({ "name": "name", "description": "description","code":"code" }),
contentType: "application/json; charset=utf-8"

Server side

[WebMethod(true)]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public static String Save(string name, string description, string code)
{
}
Sign up to request clarification or add additional context in comments.

5 Comments

I coaught the paramaters in webmethod Save(string name, string description, string code). and I used querystring for pageloading. Is it working for both?
It's a web service so parameters wouldn't be passed in the Querystring. The first line of code you give him would solve his problem - no need for the rest as it's incorrect or guiding him the wrong way.
@Archer url: "Info.aspx/Save" is it a web service ? for web service data must be JSON.stringify(data).
[WebMethod(true)] Just this is enough for my code. thanks (:
Sorry - of course it's not a web service - my bad. But if he wants to pass the data as parameters, as in this question, then he simply passes an object and jQuery will do the work for him, so converting it is not required.

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.