Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

jQ 基础取值问题, 如果用 Angular 呢? 哪个效率更高?

如下结构是请求后台,打印出的 res 返回数据,JQ如何取出 token 的值 ?
如果用 Angular做呢 ? 哪个效率高

image

function myFunction(){
            $.ajax({
                url : 'http://49.233.117.26/api/3.8/auth/signin',
                headers: {'content-type': 'text/xml'},
                data:'<tsRequest>' + '<credentials name="admin" password="admin" >' +
                    '<site contentUrl=""/>' + '</credentials>' + '</tsRequest>',
                type : 'post',
                crossDomain: true,
                   async : false,
                cache : false,
                success : function (res){
                    console.log(res);
                    // 取值部分


                }
        });

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

答案是一样高,不过 jQuery 的代码可以短很多:

$(XML片段字符串).find('credentials').attr('token');

这其实是利用了 XML 的结构与 HTML 高度类似的特点,把 XMLHTML 来解析。
官方的解析方法是把 XMLXMLDocument 来解析, jQuery 貌似有 XML 相关的 API ,但是没认真学过 jQuery所以不记得了。
现代框架基本都不考虑 XML 了,如果没有现成的 jQuery 的话,就直接适使用 DOM 接口:

const parser = new DOMParser();
const frag = parser.parseFromString(XMLString, 'text/xml');
const tocken = frag.querySelector('credentials').getAttribute('token');

jQuery 估计也是这么做的,但是代码明显短很多。


按道理说 res 应该已经是 XMLDocument 节点对象,可以直接应用 DOM 接口的。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...