天天看点

ajax web服务调用无效 参数值,无效的Web服务调用,缺少参数值:\ u0027sentQuery \ u0027&无效的JSON原语(Invalid web service call, mis...

无效的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

...