Tôi có jquery gọi một webservice asp.net với một số nguyên. Trên ứng dụng legaacy của chúng tôi đã được chuyển sang .net 4.0 Tôi không thể thực hiện cuộc gọi này. tôi có thể gọi một phương thức mà không có thông số nhưng gửi dữ liệu đến phương pháp web sẽ trả về lỗi sau:jQuery Ajax tới dịch vụ web asp.net asmx ném Yêu cầu định dạng không hợp lệ: application/json
System.InvalidOperationException: Request format is invalid: application/json; charset=UTF-8.
at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
Tôi tạo ra chính xác cùng mã trong một dự án trống và nó làm việc tốt. Tôi không thể thấy bất cứ điều gì trong web.config rằng dự án trống được thêm vào sẽ tạo sự khác biệt.
Bộ luật Jquery
$.ajax({
type: "POST",
url: "/WebService1.asmx/Test",
data: JSON.stringify({"code": 1234}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
});
My Web Service Mã
<ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1
Inherits WebService
<WebMethod()>
Public Function Test(ByVal code As Integer) As String
Return "success"
End Function
<WebMethod()>
Public Function Hello() As String
Return "hello"
End Function
End Class
Web Config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
</appSettings>
<connectionStrings>
</connectionStrings>
<system.web>
<httpRuntime enableVersionHeader="false" />
<httpCookies httpOnlyCookies="true" requireSSL="false" lockItem="true" />
<trace enabled="false" pageOutput="true" requestLimit="40" localOnly="true"/>
<httpModules>
</httpModules>
<compilation debug="true" strict="true" explicit="true" targetFramework="4.0">
</compilation>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
</pages>
<authentication mode="Forms">
<httpHandlers>
</httpHandlers>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules>
</modules>
<handlers>
</handlers>
<httpErrors errorMode="Custom" >
</httpErrors>
</system.webServer>
</configuration>
Bạn có thể đăng tệp web.config của mình không? Tôi biết vấn đề ở đâu, chỉ cần xác nhận trước khi tôi đăng câu trả lời. –
@DarinDimitrov Tôi đã thêm web.config nhưng đã xóa các chi tiết nhạy cảm – skyfoot