asp.net webservice jquery填充文本框 [英] asp.net webservice jquery populate textbox

查看:71
本文介绍了asp.net webservice jquery填充文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

现在,我想使用jQuery将这3个值放入3个不同的文本框中.txtOrgCity=城市,txtPin = pin,txtCountry =国家

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

bellow是txtOrgCity的代码

bellow is code for 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);
                        }
                    });

当我运行它时,它会在文本框中显示[object object].

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

我如何定义在响应中(data.d)为txtOrgCity抓取City,为txtOrgPin抓取引脚,为txtOrgCountry抓取国家/地区??

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

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

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

给出的代码仅用于某些txtbox自动完成功能,并且可以完美运行,因此我只想对其进行一些修改即可. $('#txtOrgCity').val为$('#txtOrgCity').autocomplete

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

任何帮助将不胜感激.

- 谢谢

推荐答案

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

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.

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

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.

有了这些信息,就很容易使用jquery并对数据进行所需的操作.因此,在ajax调用的成功部分中,您可以编写:

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:

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

p.s.我正在电话里写东西.

p.s. im writting on a phone.

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

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 :

 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);
                    }
                });
 }

这篇关于asp.net webservice jquery填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆