asp.net web服务使用SOAP与jquery ajax获取错误

时间:2017-10-30 16:57:43

标签: jquery asp.net ajax web-services soap

当我使用ajax调用Web服务时出现错误,我尝试搜索所有内容但我无法解决此问题。

下面是我的网络服务类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Web.Script.Serialization;

/// <summary>
/// Summary description for WebServiceHome
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]

public class WebServiceHome : System.Web.Services.WebService {

    public WebServiceHome () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }
    [WebMethod]
    public int converttodaysweb(int day, int month, int year)
    {
        DateTime dt = new DateTime(year, month, day);
        int datetodays = DateTime.Now.Subtract(dt).Days;
        return datetodays;

    }

    [System.Web.Script.Services.ScriptMethod()]
    [WebMethod]
    public string GetData()
    {
        Dictionary<string, string> name = new Dictionary<string, string>();
        name.Add("1", "Rashid");
        name.Add("2", "Burhan");
        string myJsonString = (new JavaScriptSerializer()).Serialize(name);

        return myJsonString;
    }

}

它可以正常使用调用按钮,但是当我尝试使用jquery ajax调用它时工作正常。

Invoke click button Image

但是当我尝试使用jquery ajax调用上面的方法时我得到了一些错误,下面是我的jquery ajax调用

var GetJson = function () {
            var requestdata = {};
            requestdata.id = "";
            var pdata = "";
            var Requrl = "http://localhost:51120/WebServiceNew/WebServiceHome.asmx/GetData";
            $.ajax({
                type: "POST",
                url: Requrl,
                crossDomain: true,
                contentType: "application/json",
                dataType: "json",
                success: function (data) {
                    alert(data);
                },
                error: function (xhr) {
                    console.log(xhr);
                }
            });
        }

我收到以下错误

Ajax calling error

Jquery Ajax Error

请检查我的 webconfig

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
  </configSections>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <webServices>
      <protocols>
        <add name="HttpSoap"/>
        <add name="HttpPost"/>
        <add name="HttpGet"/>
        <add name="HttpPostLocalhost"/>
        <!-- Documentation enables the documentation/test pages -->
        <add name="Documentation"/>
      </protocols>

    </webServices>

  </system.web>
  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Headers" value="*" />
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="*" />
      </customHeaders>
    </httpProtocol>
    <handlers>
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </handlers>
  </system.webServer>
</configuration>

0 个答案:

没有答案