我在從ReactJS獲取和傳遞資料到ASP.NET Web服務時遇到了一個問題。
當只從資料庫中獲取資料時,其他的獲取功能是有效的。
為什么我會出現這些錯誤?我在這里做錯了什么?
我相信是我的signUp fetch函式的問題。
這個回應我認為是fetch不能從服務器上讀取回應-
。
main.chunk。 js:2417 POST http:/ localhost:50496/WebService. asmx/SignUp 500(內部服務器錯誤)。
SyntaxError。Unexpected token T in JSON at position 0 at JSON. parse (<anonymous>)
ReactJS
userData = {
fName: 'some',
lName: '兩個一'。
email: '[email protected]',
password: 'se123456' }
我在ReactJs中的fetch函式 -
const signUp = (signUpData)=> {
let data = {
firstName: signUpData.fName,
lastName: signUpData.lName,
emailAddress: signUpData.email,
password: signUpData.password,
};
fetch('http://localhost:50496/WebService.asmx/SignUp'/span>, {
body: JSON.stringify(資料)。
method: 'POST'。
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.text()
.then(
(xml) =>
new window.DOMParser()。 parseFromString(xml, 'text/xml')
.documentElement.firstChild.textContent。
)
.then((jsonStr) => JSON。 parse(jsonStr))
.then((userData) =>/span> {
if (userData.d && userData.d.length > 0) {
setUser(userData)。
} else {
console.log('error no data'/span>)。
}
})
.catch((err) => {
console.log(err)。
});
};
來自Chrome的錯誤 -
System.InvalidOperationException。缺少引數:firstName。 System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) System.Web.Services.Protocols.UrlParameterReader.Read(HttpRequest request) System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
。
SignUp WebMethod在WebService中作業。
[WebMethod]
public string SignUp(string firstName, string lastName, string emailAddress, string password)
{
return BLL.SignUp(firstName, lastName, emailAddress, password)。
uj5u.com熱心網友回復:
我找到了我需要做的事情,
另一個錯誤是 -
InvalidOperationException。只有類定義中帶有 [ScriptService] 屬性的 Web 服務 屬性的 Web 服務才能從腳本中讀取。無效操作例外:只有類定義中帶有 [ScriptService] 屬性的 Web 服務才能從腳本中讀取。
于是我在WebService.cs中加入了屬性類[ScriptService]
[WebService(Namespace = "http://tempuri.org/"/span>)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1) ]
[ScriptService] 。
由于這完全消除了在使用標準SOAP(簡單物件訪問協議)網路服務時必須決議XML的需要,
我洗掉了這幾行代碼--
.then((response) => response.text()
.then(
(xml) =>
new window.DOMParser()。 parseFromString(xml, 'text/xml')
.documentElement.firstChild.textContent。
)
.then((jsonStr) => JSON。 parse(jsonStr))
然后改成這樣 -
.then((response) => response.json()
.then((userData) => {
if (userData.d && userData.d.length > 0) {
let user = JSON.parse(userData.d)。
setUserData(user)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/312419.html
標籤:

