ฉันได้รับการติดต่อกับ Webservice มาเป็นเวลานาน แต่มีบทช่วยสอนไม่มากสำหรับการใช้ ASP เพื่อโทรหาและมันไม่ได้เป็นเรื่องปกติที่จะแปลงเป็น JSON
ก่อนกำหนดข้อมูล SOAP จากนั้นสร้างวัตถุ HTTP จากนั้นส่งโดยใช้โพสต์และรับรหัสสถานะ 200 ซึ่งหมายความว่าการโทรสำเร็จแล้วดำเนินการต่อไปยังการดำเนินการถัดไป ...
ดูรหัสการใช้งานเฉพาะ
<!-#รวม virtual =/include/json.min.asp-> <%dim strxmldim str'definition soap message strxml = <? xml version = '1.0' encoding = 'utf-8'?> strxml = strxml & <SOAP: ซอง XMLNS: XSI = 'http: //www.w3.org/2001/xmlschema-instance' xmlns: xsd = 'http: //www.w3.org/2001/xmlschema' http://schemas.xmlsoap.org/soap/envelope/ '> strxml = strxml & <soap: body> strxml = strxml & <getCategories xmlns = 'http: //tempuri.org/'> strxml = strxml & <reqcode> 30000001 </reqcode> 'parameter 2strxml = strxml & </getCategories> strxml = strxml & </soap: body> strxml = strxml & </soap: ensolope> set h = createObject (microsoft.xmlhttp)' ส่งข้อความโพสต์ h.open โพสต์, http://www.domain.com/webservice.asmx, falseh.setrequestheader ประเภทประเภท, ข้อความ/xml; .org/getCategoriessh.send (strxml) 'แสดงข้อมูล XML ที่ส่งคืนถ้า h.status = 200 จากนั้นตั้งค่า xmldoc = server.createObject (msxml.domDocument) xmldoc.load (h.Responsexml) jsonText = xmldoc.childNodes (1) .Text 'แปลงเป็น JSON โทรหา json.min.asp หมวดหมู่ = json.parse (jsontext) สำหรับ i = 0 ถึงหมวดหมู่. i) .categoryName)) 'คุณสมบัติสำหรับ j = 0 ถึง categories.get (i) .labels.length - 1' การตอบสนองการรวบรวม. เขียน (cstr (categories.get (i) .labels.get (j) .labelname)))))) ถัดไปถัดไป <ชื่อก่อน = code class = vb> <สคริปต์ language = javascript runat = เซิร์ฟเวอร์> if (! array.prototype.get) {array.prototype.get = function (prop) {ส่งคืนนี้ [prop];}} ใช้อย่างเข้มงวด; ถ้า (! this.json) {json = { };} (function () {function f (n) {return n <10? '0'+n: n;} ถ้า (typeof date.prototype.tojson! == 'function') {date.prototype.tojson = ฟังก์ชัน (คีย์) {return isfinite (this.valueof ())? this.getUtculyear ()+'-'+f (this.getUtcmonth ()+1)+'-'+f (this.getUtcDate ())+'t'+f (นี่ .getUtChours ())+':'+f (this.getUtcMinutes ())+':'+f (this.getUtcSeconds ())+'z': null;}; string.prototype.tojson = number.prototype tojson = boolean.prototype.tojson = function (key) {return this.valueof ();};} var cx =/[/?/?/?-/?/?/?/?/?-/?/?-/?-/?/?-/?/?-/?/?-/?] /g, escapable =/[//// x00-/x1f/x7f-/x9f/?/?-/?/?/?/?/?-/?-/?/?-/?/? -/?/?-/?/?-/?/?-/?]/g, ช่องว่าง, เยื้อง, meta = {'/b': '// b', '/t': '// t' , '/n': '// n', '/f': '// f': '// f', '/r': '// r', '': '//', '// /':' /// '}, Rep; Function Quote (String) {Escapable.LastIndex = 0; return Escapable.test (String)? ''+string.replace (หลบหนี, ฟังก์ชั่น (a) {var c = meta [a]; return typeof c === 'สตริง'? c: '// u'+('0000 '+A.Charcodeat (0) .ToString (16)). slice (-4);})+' ':' '+สตริง+' ';} ฟังก์ชั่น str (คีย์, ตัวยึด) {var i, k, v, ความยาว, mind = gap, บางส่วน, value = holder [key]; if (value && typeof value === 'Object' && typeof value.tojson === 'function') {value = value.tojson (คีย์);} ถ้า (typeof rep === 'function') {value = rep.call (ผู้ถือ, คีย์, ค่า); ค่า) {case'string ': return quote (value); case'number': return isfinite (value)? string (value): 'null'; case'boolean ': case'null': return String (value); case'Object ': if (! value) {return'null';} gap+= intent; บางส่วน = []; if (object.prototype.toString.apply (value) === '[วัตถุ array] ') {length = value.length; สำหรับ (i = 0; i <length; i+= 1) {บางส่วน [i] = str (i, value) ||' null ';} v = partial.length = == 0? '[]': ช่องว่าง? '[/n'+gap+partial.join (',/n'+gap)+'/n'+mind+']': '['+partial.join ( ',')+']'; gap = mind; return v;} ถ้า (rep && typeof rep === 'object') {length = rep.length; สำหรับ (i = 0; i <length; i+= 1) { k = rep [i]; ถ้า (typeof k === 'String') {v = str (k, value); if (v) {partial.push (quote (k)+(Gap? ':': ':')+v);}}} } else {สำหรับ (k ในค่า) {if (object.hasownproperty.call (value, k)) {v = str (k, value); ถ้า (v) {partial.push (อ้างอิง (k)+(ช่องว่าง? ': ':': ')+v);}}}}}} v = partial.length === 0?' {} ': Gap?' {/n '+gap+partial.join (',/n '+ Gap)+'/n'+mind+'}': '{'+partial.join (',')+'}'; gap = mind; return v;}} ถ้า (typeof json.stringify! == 'ฟังก์ชั่น ') {json.stringify = function (value, replacer, space) {var i; gap =' 'indent =' 'ถ้า (typeof space ===' number ') {สำหรับ (i = 0; i <space ; i+= 1) {Intent+= ' ';}} อื่นถ้า (typeof space ===' string ') {intent = space;} rep = replacer; if (replacer && typeof replacer! ==' function '&& (typeof replacer! ==' object ' .length! == 'number')) {โยนข้อผิดพลาดใหม่ ('json.stringify');} return str ('', {'': value});};} ถ้า (typeof json.parse! == ' function ') {json.parse = function (text, reviver) {var j; ฟังก์ชั่น Walk (Holder, Key) {var k, v, value = holder [key]; if (value && typeof value === 'object') {สำหรับ (k in value) {ถ้า (object.hasownproperty.call (value, k) ) {v = walk (value, k); if (v! == undefined) {value [k] = v;} else {ลบค่า [k];}}}} return reviver.call (ผู้ถือ, คีย์, ค่า);} cx.lastindex = 0; ถ้า (cx.test (ข้อความ)) {text = text.replace (cx, ฟังก์ชั่น (a) {return '// u'+(' 0000 '+A.Charcodeat (0) .ToString (16)). slice (-4);});} ถ้า (/^[/] ,: {}/s]*$/. ทดสอบ (ข้อความ แทนที่ (/// (?: [//// bfnrt] | u [0-9a-fa-f] {4})/g, '@') แทนที่ (/[^/// n/r] *| true | false | null |-?/d+(?: /./ d*)? (?: [ee] [+/-]?/d+)?/g, ']') แทนที่ (/(/( ?:^|: |,) (?:/s*/[)+/g, '')))) {j = eval ('('+ข้อความ+')'); return typeof reviver === 'function'? walk ({'': j}, ''): j;} โยน syntaxerror ใหม่ ('json.parse');};}} ()); </script> ตั้ง xmldoc = ไม่มีอะไรตอบสนอง สิ่งที่แนบมา: json.min.asp <script language = javascript runat = เซิร์ฟเวอร์> if (! array.prototype.get) {array.prototype.get = function (prop) {ส่งคืนนี้ [prop];}} ใช้อย่างเข้มงวด; ถ้า (! this.json) {json = {};} (ฟังก์ชัน () {ฟังก์ชัน f (n) {return n <10? '0'+n: n;} ถ้า (typeof date.prototype.tojson! == 'function') {date.prototype tojson = function (key) {return isfinite (this.valueof ())? this.getUtculyear ()+'-'+f (this.getUtcmonth ()+1)+'-'+f (this.getUtcDate ())+'t'+f (นี่ .getUtChours ())+':'+f (this.getUtcMinutes ())+':'+f (this.getUtcSeconds ())+'z': null;}; string.prototype.tojson = number.prototype tojson = boolean.prototype.tojson = function (key) {return this.valueof ();};} var cx =/[/?/?/?-/?/?/?/?/?-/?/?-/?-/?/?-/?/?-/?/?-/?] /g, escapable =/[//// x00-/x1f/x7f-/x9f/?/?-/?/?/?/?/?-/?-/?/?-/?/? -/?/?-/?/?-/?/?-/?]/g, ช่องว่าง, เยื้อง, meta = {'/b': '// b', '/t': '// t' , '/n': '// n', '/f': '// f': '// f', '/r': '// r', '': '//', '// /':' /// '}, Rep; Function Quote (String) {Escapable.LastIndex = 0; return Escapable.test (String)? ''+string.replace (หลบหนี, ฟังก์ชั่น (a) {var c = meta [a]; return typeof c === 'สตริง'? c: '// u'+('0000 '+A.Charcodeat (0) .ToString (16)). slice (-4);})+' ':' '+สตริง+' ';} ฟังก์ชั่น str (คีย์, ตัวยึด) {var i, k, v, ความยาว, mind = gap, บางส่วน, value = holder [key]; if (value && typeof value === 'Object' && typeof value.tojson === 'function') {value = value.tojson (คีย์);} ถ้า (typeof rep === 'function') {value = rep.call (ผู้ถือ, คีย์, ค่า); ค่า) {case'string ': return quote (value); case'number': return isfinite (value)? string (value): 'null'; case'boolean ': case'null': return String (value); case'Object ': if (! value) {return'null';} gap+= intent; บางส่วน = []; if (object.prototype.toString.apply (value) === '[วัตถุ array] ') {length = value.length; สำหรับ (i = 0; i <length; i+= 1) {บางส่วน [i] = str (i, value) ||' null ';} v = partial.length = == 0? '[]': ช่องว่าง? '[/n'+gap+partial.join (',/n'+gap)+'/n'+mind+']': '['+partial.join ( ',')+']'; gap = mind; return v;} ถ้า (rep && typeof rep === 'object') {length = rep.length; สำหรับ (i = 0; i <length; i+= 1) { k = rep [i]; ถ้า (typeof k === 'String') {v = str (k, value); if (v) {partial.push (quote (k)+(Gap? ':': ':')+v);}}} } else {สำหรับ (k ในค่า) {if (object.hasownproperty.call (value, k)) {v = str (k, value); ถ้า (v) {partial.push (อ้างอิง (k)+(ช่องว่าง? ': ':': ')+v);}}}}}} v = partial.length === 0?' {} ': Gap?' {/n '+gap+partial.join (',/n '+ Gap)+'/n'+mind+'}': '{'+partial.join (',')+'}'; gap = mind; return v;}} ถ้า (typeof json.stringify! == 'ฟังก์ชั่น ') {json.stringify = function (value, replacer, space) {var i; gap =' 'indent =' 'ถ้า (typeof space ===' number ') {สำหรับ (i = 0; i <space ; i+= 1) {Intent+= ' ';}} อื่นถ้า (typeof space ===' string ') {intent = space;} rep = replacer; if (replacer && typeof replacer! ==' function '&& (typeof replacer! ==' object ' .length! == 'number')) {โยนข้อผิดพลาดใหม่ ('json.stringify');} return str ('', {'': value});};} ถ้า (typeof json.parse! == ' function ') {json.parse = function (text, reviver) {var j; ฟังก์ชั่น Walk (Holder, Key) {var k, v, value = holder [key]; if (value && typeof value === 'object') {สำหรับ (k in value) {ถ้า (object.hasownproperty.call (value, k) ) {v = walk (value, k); if (v! == undefined) {value [k] = v;} else {ลบค่า [k];}}}} return reviver.call (ผู้ถือ, คีย์, ค่า);} cx.lastindex = 0; ถ้า (cx.test (ข้อความ)) {text = text.replace (cx, ฟังก์ชั่น (a) {return '// u'+(' 0000 '+A.Charcodeat (0) .ToString (16)). slice (-4);});} ถ้า (/^[/] ,: {}/s]*$/. ทดสอบ (ข้อความ แทนที่ (/// (?: [//// bfnrt] | u [0-9a-fa-f] {4})/g, '@') แทนที่ (/[^/// n/r] *| true | false | null |-?/d+(?: /./ d*)? (?: [ee] [+/-]?/d+)?/g, ']') แทนที่ (/(/( ?:^|: |,) (?:/s*/[)+/g, '')))) {j = eval ('('+ข้อความ+')'); return typeof reviver === 'function'? walk ({'': j}, ''): j;} โยน syntaxerror ใหม่ ('json.parse');};}} ()); </script>