繁体   English   中英

为什么从Ajax调用Web服务会引发内部服务器500错误?

[英]Why calling a webservice from ajax throws internal server 500 error?

我使用ajax $ .post方法将数据发送到我的.asmx Web服务,但是会引发内部服务器500错误。

.asmx.cs

namespace WebServices
{
  /// <summary>
  /// Summary description for himher
  /// </summary>
  [WebService(Namespace = "http://tempuri.org/")]
  [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 himher : System.Web.Services.WebService
  {
    [WebMethod(EnableSession = true)]
    //[ScriptMethod(UseHttpGet = false)]
    public string InsertUsers(string name, string pwd)
    {
      basicoperation bop = new basicoperation();
      return bop.insertUsers(name, pwd);
    }
  }
}
function save() {
  $("button").click(function() {
    $.post("http://localhost:8373/himher.asmx?op=InsertUsers", {
      name: "testing ajax name", 
      pwd: "testing ajax pwd"
    }, function(data, status) {
      alert(data);
    });
  });
}

我收到此错误:

加载资源失败:服务器响应状态为500(内部服务器错误)

我还为chrome添加了扩展,例如Allow-Control-Allow-Origin

我已经花了很多时间,而且还不能解决。 帮助我。 我是AJAX的新手。

是的,我已经测试了Web服务,并且它可以正常工作,例如将数据发送到SQL表。 C#的末尾没有错误。 它也不例外。

我尝试在IIS Express和Windows IIS上运行它,并且都返回相同的错误。 救命。

更新:insertusers

 public string insertUsers(string Name, string Password)
        {
                string status;

                String ConStr = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

                SqlConnection sqlCon = new SqlConnection(ConStr);    // to make a connection with DB 

                SqlCommand sqlCom = new SqlCommand("InsertUsers", sqlCon); // now in order to perform action such as insert SP, we must create command object which needs command name and conncetion only

                sqlCom.CommandType = CommandType.StoredProcedure;  // you must tell the system that insertInfo is a storedprocedure 

                SqlParameter sqlParamName = new SqlParameter("@UserName", Name);
                SqlParameter sqlParamPwd= new SqlParameter("@Password", Password);

                sqlCom.Parameters.Add(sqlParamName);
                sqlCom.Parameters.Add(sqlParamPwd);      

                try
                {
                    sqlCon.Open();

                    sqlCom.ExecuteNonQuery();   // executenonquery is used for INSERT, UPDATE, DELETE 

                    //sqlCom.ExecuteScalar();   // used to pick or read a single value from procedure
                   // Response.Write("Done");

                    sqlCon.Close();

                    status= "Success";
                }
                catch (Exception ex)
                {
                    //response.Write(ex.Message);
                    status = ex.Message;
                }
            return status;
        }

堆栈跟踪:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><soap:Code><soap:Value>soap:Receiver</soap:Value></soap:Code><soap:Reason><soap:Text xml:lang="en">System.Web.Services.Protocols.SoapException: Server was unable to process request. ---&gt; System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
   at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
   at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read()
   at System.Xml.XmlReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent()
   at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement()
   at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest()
   at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message)
   at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
   at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean&amp; abortProcessing)
   --- End of inner exception stack trace ---</soap:Text></soap:Reason><soap:Detail /></soap:Fault></soap:Body></soap:Envelope>

解决了。 我想通了,这肯定是一个JavaScript问题。

唯一需要做的就是删除 来自网址,例如

"http://localhost:8373/himher.asmx/InsertUsers",

代替

"http://localhost:8373/himher.asmx?op=InsertUsers"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM