asp.net webservice jquery填充文本框

时间:2021-01-03 03:15:57

I want to get 3 values from a web service by providing current URL and current user. My webservice accepts 2 parameter URL and user. Web service works fine.

我想通过提供当前URL和当前用户从Web服务获得3个值。我的webservice接受2个参数URL和用户。 Web服务工作正常。

Now I want to put this 3 values in 3 different textboxes using jquery.in txtOrgCity = city, in txtPin = pin, in txtCountry = country

现在我想使用jquery.in txtOrgCity = city,在txtPin = pin,txtCountry = country中将这3个值放在3个不同的文本框中

bellow is code for txtOrgCity

bellow是txtOrgCity的代码

$(document).ready(function () {
            $('#txtOrgCity').val({
                source: function (request, response) {
                    $.ajax({
                        url: '../lead_hunter.asmx/GetOrgCity',
                        method: 'post',
                        contentType: 'application/json;charset=utf-8',
                        data: JSON.stringify({ url: "http://srv3.testsrv.com/homepage", user: "testuser@testsrv.com" }),
                        dataType: 'json',
                        success: function (data) {
                            response(data.d);

                        },
                        error: function (err) {
                            alert(err);
                        }
                    });

when I run it gives me [object object] in text box.

当我运行它时,在文本框中给我[对象对象]。

How do I define to grab City for txtOrgCity, pin for txtOrgPin, country for txtOrgCountry in response(data.d).?

如何定义为txtOrgCity获取City,为txtOrgPin定位,为响应(data.d)中的txtOrgCountry获取国家/地区。

and do I need to duplicate the same code for other 2 text boxes or any better way.?

我是否需要为其他2个文本框或更好的方法复制相同的代码。

Given code is just for some txtbox autocomplete and it works perfectly so I just wanted it to modify a bit for my need. $('#txtOrgCity').val was $('#txtOrgCity').autocomplete

鉴于代码仅适用于某些txtbox自动完成,并且它完美运行所以我只是想让它根据我的需要进行修改。 $('#txtOrgCity')。val是$('#txtOrgCity')。自动完成

Any help would be appreciated.

任何帮助,将不胜感激。

-- Thanks

3 个解决方案

#1


1  

I recommend that you open this up in google chrome. Open up your developer tools (press f12) and, open up resources and select the page you are currently working on. Then use the find box to search for your javascript method which fires the ajax and put a break point inside the success part of your ajax call. Now run your code and wait to hit the break point. Now you can see what is inside your data.d object. Do not resume and keep debugging. Open up your console tab and type data.d. you should see an intelicence option box with all the variables inside your data.d object. You should see the variable for city, pin and country in whatever way you named them when you deserialized your data and returned it as json to your ajax call.

我建议您在谷歌浏览器中打开它。打开开发人员工具(按f12键),打开资源并选择当前正在处理的页面。然后使用查找框搜索您的javascript方法,该方法触发ajax并在ajax调用的成功部分中放置一个断点。现在运行您的代码并等待达到断点。现在,您可以看到data.d对象中的内容。不要恢复并继续调试。打开控制台选项卡并键入data.d.你应该看到一个intelicence选项框,其中包含data.d对象中的所有变量。当您反序列化数据并将其作为json返回到ajax调用时,您应该以任何方式查看city,pin和country的变量。

If, for example, you write data.d.city in your console it should write out the corresponding value. The same goes for any other json variable your service passed back to the browser.

例如,如果在控制台中编写data.d.city,则应写出相应的值。对于您的服务传递回浏览器的任何其他json变量,情况也是如此。

With that information it is easy enough to use jquery and do what you want with the data. So in the succes part of your ajax call you can write:

使用该信息,使用jquery并使用数据执行所需操作非常简单。所以在你的ajax调用的成功部分你可以写:

 $("#txtOrgCity").val(data.d.city);
 $("#txtPin").val(data.d.pin);
 $("#txtCountry").val(data.d.country);

p.s. im writting on a phone.

附:我正在写电话。

For your example you should not write out the same code two more times. Do not call ajax inside a jquery .val(), that is wrong. Make a new function which handles your ajax, call it from the page load or anywhere you need :

对于您的示例,您不应该再写两次相同的代码。不要在jquery .val()中调用ajax,这是错误的。创建一个处理ajax的新函数,从页面加载或任何你需要的地方调用它:

 function loadData(//put your user parameter in here if you need){
 $.ajax({
                    url: '../lead_hunter.asmx/GetOrgCity',
                    method: 'post',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify({ url: "http://srv3.testsrv.com/homepage", user: "testuser@testsrv.com" }),
                    dataType: 'json',
                    success: function (data) {                   
                         $("#txtOrgCity").val(data.d.city);
                         $("#txtPin").val(data.d.pin);
                         $("#txtCountry").val(data.d.country);                               
                     },
                    error: function (err) {
                        //welldone for handling your error message. Many people neglect this. As a developer that is like coding blindly. At the very least you can console.log(err); if you don't want the user to see
                        alert(err);
                    }
                });
 }

#2


1  

Instead of $('#txtOrgCity').val({})

而不是$('#txtOrgCity')。val({})

In your .ready function make the AJAX call first. Store d.OrgCity, d.OrgPin & d.OrgCountry into some local JavaScript variables. In the Ajax call success use these values to deposit into textboxes like

在.ready函数中首先进行AJAX调用。将d.OrgCity,d.OrgPin和d.OrgCountry存储到一些本地JavaScript变量中。在Ajax调用成功中使用这些值存放到文本框中

$('#txtOrgCity').val(d.OrgCity)

#3


0  

You are after JSON.stringify(). So where you specify your .val() You want .val(JSON.stringify());

你是在JSON.stringify()之后。所以你指定你的.val()你想要.val(JSON.stringify());

#1


1  

I recommend that you open this up in google chrome. Open up your developer tools (press f12) and, open up resources and select the page you are currently working on. Then use the find box to search for your javascript method which fires the ajax and put a break point inside the success part of your ajax call. Now run your code and wait to hit the break point. Now you can see what is inside your data.d object. Do not resume and keep debugging. Open up your console tab and type data.d. you should see an intelicence option box with all the variables inside your data.d object. You should see the variable for city, pin and country in whatever way you named them when you deserialized your data and returned it as json to your ajax call.

我建议您在谷歌浏览器中打开它。打开开发人员工具(按f12键),打开资源并选择当前正在处理的页面。然后使用查找框搜索您的javascript方法,该方法触发ajax并在ajax调用的成功部分中放置一个断点。现在运行您的代码并等待达到断点。现在,您可以看到data.d对象中的内容。不要恢复并继续调试。打开控制台选项卡并键入data.d.你应该看到一个intelicence选项框,其中包含data.d对象中的所有变量。当您反序列化数据并将其作为json返回到ajax调用时,您应该以任何方式查看city,pin和country的变量。

If, for example, you write data.d.city in your console it should write out the corresponding value. The same goes for any other json variable your service passed back to the browser.

例如,如果在控制台中编写data.d.city,则应写出相应的值。对于您的服务传递回浏览器的任何其他json变量,情况也是如此。

With that information it is easy enough to use jquery and do what you want with the data. So in the succes part of your ajax call you can write:

使用该信息,使用jquery并使用数据执行所需操作非常简单。所以在你的ajax调用的成功部分你可以写:

 $("#txtOrgCity").val(data.d.city);
 $("#txtPin").val(data.d.pin);
 $("#txtCountry").val(data.d.country);

p.s. im writting on a phone.

附:我正在写电话。

For your example you should not write out the same code two more times. Do not call ajax inside a jquery .val(), that is wrong. Make a new function which handles your ajax, call it from the page load or anywhere you need :

对于您的示例,您不应该再写两次相同的代码。不要在jquery .val()中调用ajax,这是错误的。创建一个处理ajax的新函数,从页面加载或任何你需要的地方调用它:

 function loadData(//put your user parameter in here if you need){
 $.ajax({
                    url: '../lead_hunter.asmx/GetOrgCity',
                    method: 'post',
                    contentType: 'application/json;charset=utf-8',
                    data: JSON.stringify({ url: "http://srv3.testsrv.com/homepage", user: "testuser@testsrv.com" }),
                    dataType: 'json',
                    success: function (data) {                   
                         $("#txtOrgCity").val(data.d.city);
                         $("#txtPin").val(data.d.pin);
                         $("#txtCountry").val(data.d.country);                               
                     },
                    error: function (err) {
                        //welldone for handling your error message. Many people neglect this. As a developer that is like coding blindly. At the very least you can console.log(err); if you don't want the user to see
                        alert(err);
                    }
                });
 }

#2


1  

Instead of $('#txtOrgCity').val({})

而不是$('#txtOrgCity')。val({})

In your .ready function make the AJAX call first. Store d.OrgCity, d.OrgPin & d.OrgCountry into some local JavaScript variables. In the Ajax call success use these values to deposit into textboxes like

在.ready函数中首先进行AJAX调用。将d.OrgCity,d.OrgPin和d.OrgCountry存储到一些本地JavaScript变量中。在Ajax调用成功中使用这些值存放到文本框中

$('#txtOrgCity').val(d.OrgCity)

#3


0  

You are after JSON.stringify(). So where you specify your .val() You want .val(JSON.stringify());

你是在JSON.stringify()之后。所以你指定你的.val()你想要.val(JSON.stringify());