本文介绍了HTTP POST 500(内部服务器错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过我的AngularJS应用程序调用Web服务将Employee添加到sql数据库。 Http Get方法在同一个应用程序中运行良好。
错误:
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://localhost:56330/EmployeeService.asmx/AddEmployee?EmailId=a2S@s&Fname=Rohit&Gender=Male&Lname=Sharma&Locartion=sd&MobileNumber=459401421 500 (Internal Server Error)
当我在单独的浏览器中复制上述链接时,它可以工作。
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:
webserice:
namespace EmployeeApp
{
/// <summary>
/// Summary description for EmployeeService
/// </summary>
[WebService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[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();
// Console.WriteLine("" + obj.firstname);
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)
{
//get all the employees details
}
con.Close();
Context.Response.Write("success");
// return obj;
}
}
}
我尝试过:
在web.config中添加HTTPGET,HTTPPOST协议:
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
推荐答案
这篇关于HTTP POST 500(内部服务器错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!