I am trying to call a webservice to add Employee to sql database through my AngularJS application. Http Get method working fine in same application.
Error:
POST http:
When I copy the above link in separate browser, it works.
addEmployee method not working in following code:
app.provider("searchservice", function () {
this.$get = function ($http, $location) {
return {
fetchdata: function () {
var empinfo = [];
var url = "EmployeeService.asmx/GetAllEmployees";
$http.get(url).then(function (response) {
angular.copy(response.data,empinfo);
}, function (response) {
console.log("something went wrong while fetching the data");
});
return empinfo;
},
addEmployee: function (emp) {
var inputdata = { Fname: emp.firstname,
Lname: emp.lastname,
Gender: emp.gender,
MobileNumber: emp.mobileNumber,
EmailId: emp.email,
Locartion: emp.location
}
var response = $http({
method: "POST",
url: "EmployeeService.asmx/AddEmployee",
params: inputdata,
}).then(function () {
console.log("Employee Saved successfully");
$scope.allEmployees = searchservice.fetchdata();
if ($scope.allEmployees !== null)
$scope.result = 'true';
else
$scope.result = 'false';
}, function () {
console.log("some error occured");
});;
console.log(response);
return response;
}
}
}
})
webserice:
namespace EmployeeApp
{
[WebService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
public string connectionstring = ConfigurationManager.ConnectionStrings["EmployeeDB"].ConnectionString.ToString();
[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]
public void AddEmployee(string Fname, string Lname, string Gender, string MobileNumber, string EmailId, string Locartion)
{
using (SqlConnection con = new SqlConnection(connectionstring))
{
string sqlQuery = "Insert into Employee (Fname, Lname, Gender, MobileNumber,EmailId,Locartion) VALUES" +
"(@Fname, @Lname, @Gender, @MobileNumber,@EmailId,@Locartion)";
con.Open();
SqlCommand cmd = new SqlCommand(sqlQuery, con);
cmd.Parameters.AddWithValue("@Fname", Fname);
cmd.Parameters.AddWithValue("@Lname", Lname);
cmd.Parameters.AddWithValue("@Gender", Gender);
cmd.Parameters.AddWithValue("@MobileNumber", MobileNumber.ToString());
cmd.Parameters.AddWithValue("@EmailId", EmailId);
cmd.Parameters.AddWithValue("@Locartion", Locartion);
int result = cmd.ExecuteNonQuery();
if (result == 1)
{
}
con.Close();
Context.Response.Write("success");
}
}
}
What I have tried:
Adding HTTPGET,HTTPPOST protocols in web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="EmployeeDB" connectionString="Data Source=LAPTOP-4S65AVT0\SQLEXPRESS;Initial Catalog=WorkshopDB2;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
<add name="HttpPostLocalhost"/>
</protocols>
</webServices>
<compilation debug="true"/>
<httpHandlers>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpHandlers>
</system.web>
</configuration>
Searched a lot on internet, not finding any solution. Please Help