無效的Web服務調用,缺少參數值:\ u0027sentQuery \ u0027&無效的JSON原語(Invalid web service call, missing value for parameter: \u0027sentQuery\u0027 & Invalid JSON primitive)
不知道為什麼我得到這個,但我得到這個錯誤:
Invalid web service call, missing value for parameter: \u0027sentQuery\u0027
嘗試執行jQuery AJAX到我的ASPX Web服務時。
我的AJAX是這樣的:
$.ajax({
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
url: "http://localhost:7665/Service1.asmx/theQ",
data: "{\"sentQuery\":" + "\"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT\"" + "}",
success: function (data) {
console.log(data);
},
error: function (a) {
alert('ERROR: ' + a.responseText);
}
});
我的VB Web服務代碼:
_
_
Public Sub theQ(ByVal sentQuery As String)
Dim results As Object = fetchSQLQ("query", sentQuery)
Try
Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim strResponse As String = ser.Serialize(results)
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()
Catch ex As Exception
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", ex.Message.Length.ToString())
Context.Response.Write(String.Format("[ERROR: {0}]", ex.Message))
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Try
End Sub
UPDATE
我猜以下是正确的:
data: { sentQuery: "SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT" },
但是,它确實産生了另一個錯誤:
Invalid JSON primitive: SELECT.
咦????
Not sure why I am getting this but I get this error:
Invalid web service call, missing value for parameter: \u0027sentQuery\u0027
When trying to execute jQuery AJAX to my ASPX web service.
My AJAX is this:
$.ajax({
type: 'GET',
contentType: "application/json; charset=utf-8",
dataType: 'json',
url: "http://localhost:7665/Service1.asmx/theQ",
data: "{\"sentQuery\":" + "\"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT\"" + "}",
success: function (data) {
console.log(data);
},
error: function (a) {
alert('ERROR: ' + a.responseText);
}
});
And my VB web service code:
_
_
Public Sub theQ(ByVal sentQuery As String)
Dim results As Object = fetchSQLQ("query", sentQuery)
Try
Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim strResponse As String = ser.Serialize(results)
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()
Catch ex As Exception
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", ex.Message.Length.ToString())
Context.Response.Write(String.Format("[ERROR: {0}]", ex.Message))
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Try
End Sub
UPDATE
I am guessing that the following is correct:
data: { sentQuery: "SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT" },
However, it does produce another error:
Invalid JSON primitive: SELECT.
Huh????
原文:https://stackoverflow.com/questions/32998831
更新時間:2020-02-26 03:43
最滿意答案
手動建立json是容易出錯的,并且由于難以閱讀也很難調試。
讓javascript中的序列化方法(如JSON.stringify為您完成
嘗試
data: JSON.stringify({sentQuery:"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"}),
Finally got this after reading Chris Brandsma post.
AJAX code:
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
url: 'http://localhost:7665/Service1.asmx/theQ',
data: JSON.stringify({ qString: ["SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"] }),
async: true,
cache: false,
success: function (data) {
console.log(data);
},
error: function (a) {
alert('ERROR: ' + a.responseText);
}
});
VB.net code:
_
_
Public Sub theQ(qString As List(Of String))
Dim results As Object = fetchSQLQ("query", qString(0))
Try
Dim ser As New System.Web.Script.Serialization.JavaScriptSerializer()
Dim strResponse As String = ser.Serialize(results)
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", strResponse.Length.ToString())
Context.Response.Write(strResponse)
HttpContext.Current.ApplicationInstance.CompleteRequest()
Catch ex As Exception
Context.Response.Clear()
Context.Response.ContentType = "application/json"
Context.Response.AddHeader("content-length", ex.Message.Length.ToString())
Context.Response.Write(String.Format("[ERROR: {0}]", ex.Message))
HttpContext.Current.ApplicationInstance.CompleteRequest()
End Try
End Sub
相關問答
它需要是一個ScriptMethod才能從JavaScript中調用它; 我懷疑另一個問題是你的參數 - 一個ASMX方法不會做MVC方法會做的自動映射,是以你需要顯式發送一個具有相應名稱和字段的對象,或者實作一個帶有屬性的方法單獨建構一個适當的對象伺服器端。 It does need to be a ScriptMethod in order to call it from JavaScript; I suspect that the other issue is your parameters
...
問題如下:ASMX Web服務要求 始終在每次向伺服器發送請求時發送方法的所有參數 。 僅當設定了過濾器時,另一側的jqGrid(在使用工具欄過濾或搜尋的情況下)才會發送過濾器資訊(如所有者:“樣本”)。 要解決此問題,您可以Owner serializeGridData回調内的Owner (或其他)擴充将發送到伺服器的參數。 例如 serializeGridData: function (postData) {
if (postData.Owner=== undefined) postD
...
請在ajax調用中嘗試以下代碼來擷取data參數: data: {myObject: JSON.stringify($(form).serializeObject())},
Finally I got it like this: $.ajax({
type: "POST",
url: "./Service.asmx/PostAutomaticRule",
data: "{'myObject': "+formData+"}",
...
更改data: JSON.stringify(dataToSend), to data: JSON.stringify({
haha: $(".txtNoiDung").val()
}),
這假設$(".txtNoiDung")在頁面中是唯一的,如果不是,則需要另一種擷取值的機制。 我很确定你可以使用$(this).val())來擷取這種情況下的值。 Change data: JSON.stringify(dataToSend), to data: JSON.stringify({
...
當使用JSON時,所有字元串都必須用雙引号引起來" ,而不是單引号' 。 \u0027是一個單引号,可能是API所抱怨的,是以如果你替換 data: "{ 'searchTerm': '" + request.term + "' }", 同 data: '{ "searchTerm": "' + request.term + '" }', 它可能會奏效。 When using JSON, all strings have to be enclosed in double quotes ", not
...
手動建立json是容易出錯的,并且由于難以閱讀也很難調試。 讓javascript中的序列化方法(如JSON.stringify為您完成 嘗試 data: JSON.stringify({sentQuery:"SELECT OPRID FROM vwPS_BC_JOB_PERS_DAT"}),
Finally got this after reading Chris Brandsma post. AJAX code: $.ajax({
type: 'POST',
contentTyp
...
data = JSON.stringify({data: data});
詳細說明,您目前正在發送2個參數,而您的Web方法隻需要一個(命名資料)。 data = JSON.stringify({data: data});
To elaborate, you are currently sending 2 parameters, whereas your web method expects just one (named data).
嘗試在ajax params中使用processData: false發送。 jQuery預設會再次進行字元串化。 使用fiddler或類似工具檢查網絡流量,并檢視實際發送到您的Web服務的内容。 Try sending with processData: false in your ajax params. jQuery will stringify again by default. Use fiddler or similar tool to inspect network traffic
...
您正在嘗試将js字元串轉換為json,您想要的是将對象轉換為json, var dcId = JSON.stringify({ dataCenterId: e.options[idx].value });
You are trying to convert a js string to json, what you want is to convert an object to json, var dcId = JSON.stringify({ dataCenterId: e.options[i
...
嘗試按照預期的Order發送Id和Order作為整數。 var reminderObject = {
Id: parseInt($(this).attr("id"), 10),
Order: parseInt($(this).find("input[type='hidden']").val(), 10)
};
同時将wrap.d = orderArray更改為wrap.rList = orderArray因為rList是預期參數。 Try sending Id and Order
...