[英]Calling asmx web service from JavaScript yields 404
我寫了一個Web服務,我想從JavaScript調用它。 問題是我收到404錯誤,並且不確定我做錯了什么。
關於如何完成任務的文章很多。 我使用的一個是在這里 。
這是背后的代碼,是的,我確實閱讀了注釋並添加了[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輸入到Web瀏覽器中,則會得到帶有注釋的標准頁面:
The following operations are supported. For a formal definition, please review the Service Description.
* GetStockName
問題似乎出在類或方法聲明之一上。
更新(Web服務)
根據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>
UPDATE 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才能使事情與其他更改一起使用。
這是我看到的一些鏈接: 鏈接1 , 鏈接2 ,更重要的是鏈接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)]
根據鏈接和SO chat中的討論,將標頭添加到服務中以指示該呼叫符合HTTP 1.1的標頭。 此標頭等效於PHP標頭。
使用IE11在Win7-64上完成測試。 如一個鏈接所述,IE11支持CORS。
(問題已更新以反映答案。)
我有代碼可以工作!
我不確定代碼何時開始工作,因為我不是一直通過IE11進行測試,而是通過啟用了JavaScript的頁面通過VS2013以調試模式與CORS頁面進行通信來進行測試。 顯然,Visual Studio禁用了對ASMX頁面的CORS操作。 一個必須超越VS調試器模式。
所有其他更改可能也是必需的。
請嘗試以下操作:
將以下內容添加到您的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"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.