0

I am trying this :

this my javascript code :

    function insertVisitor() {
        var pageUrl = '<%=ResolveUrl("~/QuizEntry.asmx")%>'
        $.ajax({
            type: "POST",
            url: pageUrl + "/insert_Visitor",
            data: "{'name':'" + $("#txtName").val() + "','phoneno':'" +
                  $("#txtPhone").val() + "'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccessCall,
            error: OnErrorCall
        });

        return false();
    }

    function OnSuccessCall(response) {
        window.open("about.html");
    }

    function OnErrorCall(response) {
        alert(response.status + " " + response.statusText);
    }

</script>

this the html :

<form action="" method="post">
            <table>
                <tr>
                    <th>
                        <label>
                            please enter your Name :
                        </label>
                    </th>
                    <td>
                        <input id="txtName" type="text" value="" placeholder="Name" />
                    </td>
                </tr>
                <tr>
                    <th>
                        please enter your Phone No :
                    </th>
                    <td>
                        <input id="txtPhone" type="text" value="" placeholder="Phone No" />
                    </td>
                </tr>
                <tr>
                    <td>
                        &nbsp;
                    </td>
                    <td>
                    <!--<input id="btnEnter" type="button" style="background-image:~/images/pop_up/enter.png; height:40px; width:120px" />-->
                        <!--<input type="submit" name="button" id="button" value="enter" onClick="window.location='about.html'" />-->
                        <!--<a href="about.html" style="display: block">-->
                            <img src="images/pop_up/enter.png" width="120" height="40" alt="img" style="width: 120px;
                                height: 40px; position: relative; right: 0;" onclick="return insertVisitor()" />
                         <!--</a>-->
                    </td>
                </tr>
            </table>
            </form>

and this is the webservice code :

 [WebMethod]
public string insert_Visitor(string name, string phoneno)
{
    string returnvalue = "";
    if (name != "" && phoneno != "")
    {
        cmd.Parameters.Clear();
        cmd.Connection = connection;
        cmd.CommandText = "Insert_Into_VisitorLog";
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Phone", phoneno);

        Object obj = cmd.ExecuteScalar();

        if (obj != "0" || obj != "Unexpected error occurred!!")
        {
            returnvalue = Convert.ToString(obj);
        }
        else
        {
            returnvalue = Convert.ToString(obj);
        }
    }


    return returnvalue;
}

the problem that i am facing is getting an error like this

400 Bad Request 

how to solve this please help

Arindam Das
  • 699
  • 4
  • 20
  • 39

1 Answers1

0

The data part in jQuery is wrong

 data: "{'name':'" + $("#txtName").val() + "','phoneno':'" +
              $("#txtPhone").val() + "'}",

it should be

 data: {'name': $("#txtName").val()  ,'phoneno': $("#txtPhone").val() },

Edit 1

Also as you are returning string from you service so dataType is also wrong in you jquery call.

Differences between contentType and dataType in jQuery ajax function

Edit 2

   var pageUrl = '<%=ResolveUrl("~/QuizEntry.asmx")%>'+"/insert_Visitor?name="+ $("#txtName").val()+"&phoneno="+ $("#txtPhone").val() ;
    $.ajax({
        type: "POST",
        url: pageUrl,
        success: OnSuccessCall,
        error: OnErrorCall
    });

Edit 3

Get more about error detail use the following

function OnErrorCall(xhr, ajaxOptions, thrownError)
{
   alert(xhr.responseText);
}
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117