我写了一个Web服务,我想通过JavaScript调用它。问题是我收到了404错误,并且不确定我做错了什么。
有很多关于如何完成任务的文章。我使用的是here。
这是后面的代码,是的,我确实阅读了评论并添加了[ScriptService]:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using BvCommonControls;
namespace MyProject.MyWebService
{
[WebService(Namespace = "http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class svcSignin : WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public String GetStockName(String argEncrypted)
{
return js.Serialize("HelloWorld!");
}
}
}
JavaScript代码是:
// Create the URL.
var serviceUrl = "http://mywebsiteaddress.whatever/Folder/myservice.asmx";
var serviceOperation = "/GetStockName";
var serviceData = "myencodedargs";
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: serviceUrl + serviceOperation,
data: JSON.stringify(serviceData),
dataType: "json",
async: true,
success: ajaxSigninSuccess,
error: ajaxSigninError
});
function ajaxSuccess(response)
{
alert(response.d);
}
function ajaxError(response)
{
alert(response.status + " " + response.statusText);
}
readyState为4,状态为404(找不到页面)。
如果我使用复制粘贴将url输入到网络浏览器中,我会收到包含评论的标准页面:
The following operations are supported. For a formal definition, please review the Service Description.
* GetStockName
问题似乎与类或方法声明之一有关。
更新(网络服务)
根据MSDN,这是WebServices部分所在的位置。
<configuration>
<system.web>
<webServices>
<conformanceWarnings>
<remove name='BasicProfile1_1'/>
</conformanceWarnings>
</webServices>
</system.web>
</configuration>
这篇CodeProject文章展示了System.Web.Handlers.ScriptModule的用途。
<configuration>
<system.web>
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
</system.web>
</configuration>
更新2(请求RESTful更新)
我在JavaScript代码中更新了Ajax部分以符合所请求的格式。基本上,如果我在浏览器中访问该页面并单击GetStockName链接,那么我将进入显示各种SOAP格式的页面,并在底部显示HTTP POST格式。
HTTP POST
The following is a sample HTTP POST request and response. The placeholders shown need to be replaced with actual values.
POST /Folder/myservice.asmx/GetStockName HTTP/1.1
Host: www.mywebsiteaddress.whatever
Content-Type: application/x-www-form-urlencoded
Content-Length: length
argEncrypted=string
我必须对URL进行硬编码,如图所示。简单地放置一个变量,如“$ .ajax(serviceUrl {”没有用,有趣的是添加“url:serviceUrl”,作为ajax属性之一也没有用。只有“$ .ajax(''{”的构造工作
$.ajax('http://mywebsiteaddress.whatever/Folder/myservice.asmx/GetStockName'{
type: 'POST',
contentType: 'application/json',
headers: { 'Access-Control-Allow-Origin': '*' },
crossDomain: true,
data: '{argEncrypted: "' + serviceData + '"}',
dataType: "json",
async: true,
success: ajaxSuccess,
error: ajaxError
});
更新3(CORS)
问题绝对是跨域或跨域资源共享或CORS之一。旧代码使用了jsonp,它运行良好,但不支持ASMX页面。
正如我在评论中所提到的,问题的一部分也是Visual Studio。在调试模式下运行IE11的VS2013不允许CORS操作。我不得不在Visual Studio之外启动IE11以使其与其他更改一起工作。
以下是我看到的一些链接:link 1,link 2,更重要的是link 3。
web.config中的添加/更改要求:
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<configuration>
和$ .ajax更改包括添加:
type: 'POST',
contentType: 'application/json',
headers: { 'Access-Control-Allow-Origin': '*' },
crossDomain: true,
请注意
部分[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
向服务添加标头,指示调用符合HTTP 1.1,每个链接以及SO chat中的讨论。此标头等同于PHP标头。
使用IE11在Win7-64上进行测试。 IE11支持CORS,如一个链接所示。
(更新问题以反映答案。)
答案 0 :(得分:1)
我让代码工作了!
我不确定代码何时开始工作,因为我没有一直通过IE11进行测试,而是通过在调试模式下通过VS2013运行的启用JavaScript的页面与CORS页面进行通信。显然,Visual Studio禁用对ASMX页面的CORS操作。一个人必须超出VS调试器模式。
也可能需要所有其他更改。
答案 1 :(得分:0)
尝试以下方法:
将以下内容添加到您的web.config
<webServices>
<protocols>
<add name="HttpPost"/>
</protocols>
</webServices>
此外,在您的AJAX请求中,更改此行
data: JSON.stringify(serviceData)
到
data: '{argEncrypted: "' + serviceData + '"}'
还要确保您拥有以下web.config条目:
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>