1

i have 1 project webservice and 1 project web asp.net i want to insert data by json(ajax) i tested file service by code behind and it good, have error with code js file WebService1:

public bool HelloWorld(student obj) {
    SqlConnection cnn = new SqlConnection("Data Source=PHAMHOP-LAPTOP\\SQLEXPRESS;Initial Catalog=qlsv;Integrated Security=True");
    cnn.Open();
    SqlCommand cmd = new SqlCommand("insert into sinhvien(name,age) values(@name,@age)", cnn);
    cmd.Parameters.AddWithValue("name", obj.name);
    cmd.Parameters.AddWithValue("age", obj.age);
    int row = cmd.ExecuteNonQuery();
    if (row == 1){
        return true;
    } else {
        return false;
    }
}

file aspx:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    function add() {
        $.ajax({
            type: "POST",
            url: "http://localhost:51097/Service1.asmx/HelloWorld",
            data: "{'id':'1' ,'name': 'Amit', 'age': '97'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert("ok");
            }
        });
    }
</script>
<body>
    <input type='Button' value='gui' OnClick='add()'/>
</body>

It does not work.

3
  • try removing contentType: "application/json; charset=utf-8", dataType: "json", Commented Aug 6, 2015 at 5:00
  • It does not work will not help. Post the actual error message you are getting from the browser console. Commented Aug 6, 2015 at 5:17
  • it call function success, but not call service, bro @Singh Commented Aug 6, 2015 at 6:08

2 Answers 2

1

Since the returned data is not JSON contentType: "application/json; charset=utf-8", dataType: "json", is not needed and the data option does not need to be a string. Pass it as an object.

Try this

function add() {
    $.ajax({
        type: "POST",
        url: "http://localhost:51097/Service1.asmx/HelloWorld",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: {
            id: 1,
            name: 'Amit',
            age: 97
        },
        success: function (msg) {
            alert("ok");
        }
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

what does 'contentType' has to do with returned data? Isn't it to specify the data that is sent to the server rather than returned from the server?
0

Alternatively, you could try passing the JSON as a string parameter instead of as a student and deserialising it yourself using Newtonsoft or System.Web.Script.Serialization.JavaScriptSerializer.

bool HelloWorld(string obj)

instead of

bool HelloWorld(student obj)

1 Comment

try passing the JSON as a string - JSON is a string

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.