从jquery ajax GET将多个参数传递给.asmx

时间:2022-12-08 20:30:27

html

HTML

<a onclick="testGetParametersDynamic2();">fill in names and check it out</a>
<br />
<p>Enter First Name</p>
<input id="myFirstName" type="text" />
<br />
<p>Enter Last Name</p>
<input id="myLastName" type="text" />
<div id="outputGET3"></div>

c#

C#

[WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true)]
public string testGetParametersDynamic(string firstName, string lastName)
{
    string fullName = firstName + lastName;

    return fullName;
}

I have tried multiple ways of entering data bc I think this is where the problem lies

我已经尝试了多种输入数据的方法bc我认为这就是问题所在

attempt 1

尝试1

function testGetParametersDynamic2()
    {
        $.ajax(
        {
            post: 'GET',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: '{"firstName":"' + $('#myFirstName').val() + '","lastName":' +
                    $('#myLastName').val() + '"}',
            url: 'UtilitieService.asmx/TestGetParametersDynamic',
            success: function (result)
            {
                var test = result.d;
                var outputDiv = $('outputGET3');
                outputDiv.html(test);
            },

            error: function ()
            {
                alert('Fail Test Get Dynamic');
            }
        });
    }

attempt 2:

尝试2:

function testGetParametersDynamic2()
    {
        $.ajax(
        {
            post: 'GET',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: "firstName" + $('myFirstName').val() + "&lastName" + $('myLastName').val(),
            url: 'UtilitieService.asmx/TestGetParametersDynamic',
            success: function (result)
            {
                var test = result.d;
                var outputDiv = $('outputGET3');
                outputDiv.html(test);
            },

            error: function ()
            {
                alert('Fail Test Get Dynamic');
            }
        });
    }

both times I get this error:

两次我都收到这个错误:

Invalid web service call, missing value for parameter: \u0027firstName\u0027

无效的Web服务调用,缺少参数值:\ u0027firstName \ u0027

3 个解决方案

#1


14  

I hope that you use [ScriptMethod(ResponseFormat=ResponseFormat.Json)] attribute for the web method or set the same information in the web.config in case of the usage .NET 4.0.

我希望您对Web方法使用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]属性,或者在使用.NET 4.0时在web.config中设置相同的信息。

It seems to me that your first attempt was almost correct, but you should replace

在我看来,你的第一次尝试几乎是正确的,但你应该更换

data: '{"firstName":"' + $('#myFirstName').val() + '","lastName":' +
                    $('#myLastName').val() + '"}',

to the

到了

data: '{"firstName":"' + $('#myFirstName').val() + '","lastName":"' +
                    $('#myLastName').val() + '"}',

(the starting double-quote was skipped before the $('#myLastName').val()).

(在$('#myLastName')。val()之前跳过了起始双引号。

I strictly recommend you don't use manual serialization to JSON. For example if the text from $('#myFirstName').val() or $('#myLastName').val() will have '"' or '\' characters, that the characters must be escaped with additional backslash ('\') (see here). Instead of manual serialization you should use JSON.stringify function from the script json2.js which you can download from the http://www.json.org/js.html or here. In recent web browsers the function are native implemented and json2.js use the native implementation if it take place.

我严格建议您不要使用手动序列化到JSON。例如,如果来自$('#myFirstName')。val()或$('#myLastName')。val()的文本将包含'''或'\'字符,则必须使用额外的反斜杠转义字符( '\')(请参阅此处)。您应该使用json2.js脚本中的JSON.stringify函数代替手动序列化,您可以从http://www.json.org/js.html或此处下载。 Web浏览器该函数是本机实现的,如果发生,json2.js使用本机实现。

The data parameter of $.ajax could be rewritten as the following:

$ .ajax的数据参数可以重写如下:

data: {
    firstName: JSON.stringify($('myFirstName').val()),
    lastName: JSON.stringify($('myLastName').val())
}

or in some situation even as

或者在某些情况下甚至是

data: {
    firstName: function() { return JSON.stringify($('myFirstName').val()); },
    lastName: function() { return JSON.stringify($('myLastName').val()); }
}

For more information see this old answer and probably also this.

有关更多信息,请参阅此旧答案,也可能是此。

UPDATED: Sorry the correct version without the usage JSON.stringify could be without the data usage:

更新:抱歉没有使用JSON.stringify的正确版本可能没有数据用法:

url: 'UtilitieService.asmx/TestGetParametersDynamic?firstName=' +
     encodeURIComponent('"' + $('#myFirstName').val() + '"') +
     '&lastName=' + encodeURIComponent('"' + $('#myLastName').val() + '"')

I strictly recommend you to use always only the JSON.stringify version which I described above.

我严格建议你只使用我上面描述的JSON.stringify版本。

#2


2  

  • Your first attempt is passing the paramaters as a json string.
  • 您的第一次尝试是将参数作为json字符串传递。
  • Your second attempt is missing = signs.
  • 你的第二次尝试失踪=迹象。

You can pass a object to data, and jQuery will serialize it properly.

您可以将对象传递给数据,jQuery将正确地序列化它。

$.ajax(
        {
            post: 'GET',
            data: {
                firstName: $('myFirstName').val(),
                lastName: $('myLastName').val()
            },
...

#3


0  

Pass multiple parameters using json

使用json传递多个参数

data: "{'RecomendeeName':'" + document.getElementById('txtSearch').value + 
                       "'," + "'tempdata':'" +"myvalue" + "'}",

#1


14  

I hope that you use [ScriptMethod(ResponseFormat=ResponseFormat.Json)] attribute for the web method or set the same information in the web.config in case of the usage .NET 4.0.

我希望您对Web方法使用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]属性,或者在使用.NET 4.0时在web.config中设置相同的信息。

It seems to me that your first attempt was almost correct, but you should replace

在我看来,你的第一次尝试几乎是正确的,但你应该更换

data: '{"firstName":"' + $('#myFirstName').val() + '","lastName":' +
                    $('#myLastName').val() + '"}',

to the

到了

data: '{"firstName":"' + $('#myFirstName').val() + '","lastName":"' +
                    $('#myLastName').val() + '"}',

(the starting double-quote was skipped before the $('#myLastName').val()).

(在$('#myLastName')。val()之前跳过了起始双引号。

I strictly recommend you don't use manual serialization to JSON. For example if the text from $('#myFirstName').val() or $('#myLastName').val() will have '"' or '\' characters, that the characters must be escaped with additional backslash ('\') (see here). Instead of manual serialization you should use JSON.stringify function from the script json2.js which you can download from the http://www.json.org/js.html or here. In recent web browsers the function are native implemented and json2.js use the native implementation if it take place.

我严格建议您不要使用手动序列化到JSON。例如,如果来自$('#myFirstName')。val()或$('#myLastName')。val()的文本将包含'''或'\'字符,则必须使用额外的反斜杠转义字符( '\')(请参阅此处)。您应该使用json2.js脚本中的JSON.stringify函数代替手动序列化,您可以从http://www.json.org/js.html或此处下载。 Web浏览器该函数是本机实现的,如果发生,json2.js使用本机实现。

The data parameter of $.ajax could be rewritten as the following:

$ .ajax的数据参数可以重写如下:

data: {
    firstName: JSON.stringify($('myFirstName').val()),
    lastName: JSON.stringify($('myLastName').val())
}

or in some situation even as

或者在某些情况下甚至是

data: {
    firstName: function() { return JSON.stringify($('myFirstName').val()); },
    lastName: function() { return JSON.stringify($('myLastName').val()); }
}

For more information see this old answer and probably also this.

有关更多信息,请参阅此旧答案,也可能是此。

UPDATED: Sorry the correct version without the usage JSON.stringify could be without the data usage:

更新:抱歉没有使用JSON.stringify的正确版本可能没有数据用法:

url: 'UtilitieService.asmx/TestGetParametersDynamic?firstName=' +
     encodeURIComponent('"' + $('#myFirstName').val() + '"') +
     '&lastName=' + encodeURIComponent('"' + $('#myLastName').val() + '"')

I strictly recommend you to use always only the JSON.stringify version which I described above.

我严格建议你只使用我上面描述的JSON.stringify版本。

#2


2  

  • Your first attempt is passing the paramaters as a json string.
  • 您的第一次尝试是将参数作为json字符串传递。
  • Your second attempt is missing = signs.
  • 你的第二次尝试失踪=迹象。

You can pass a object to data, and jQuery will serialize it properly.

您可以将对象传递给数据,jQuery将正确地序列化它。

$.ajax(
        {
            post: 'GET',
            data: {
                firstName: $('myFirstName').val(),
                lastName: $('myLastName').val()
            },
...

#3


0  

Pass multiple parameters using json

使用json传递多个参数

data: "{'RecomendeeName':'" + document.getElementById('txtSearch').value + 
                       "'," + "'tempdata':'" +"myvalue" + "'}",