如何在XMLHttpRequest中传递参数 [英] How to pass parameters in XMLHttpRequest

查看:144
本文介绍了如何在XMLHttpRequest中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是示例代码,想要在C#服务器端创建此Java脚本函数.卡住了如何传递参数"rdverb = INFO& URL ="到c#中的http侦听器.

Here is the sample code and want to create this java script function in C# server side. Stuck in how to pass parameter "rdverb=INFO&URL=" to the http listener in c#.

function Info(){
if(PortNumber === XXXX){
alert("Message.");
return;
}
            var xmlHttpD = new XMLHttpRequest();
xmlHttpD.open("INFO","http://127.0.0.1:" + PortNumber +"/getInfo",true);

            xmlHttpD.onload =函数(e){
               如果(xmlHttpD.readyState === 4){
                   如果(xmlHttpD.status === 200){
                        alert(xmlHttpD.responseText);
                    }其他{
                        alert(xmlHttpD.statusText);
                    }
                }
            };
            xmlHttpD.onerror =函数(e){
                console.error(xmlHttpD.statusText);
            };

            var params =" rdverb = INFO& URL =''" ;;
            xmlHttpD.send(params);
        }

function Info() {
if(PortNumber === XXXX){
alert("Message.");
return;
}
            var xmlHttpD = new XMLHttpRequest();
xmlHttpD.open("INFO", "http://127.0.0.1:" + PortNumber + "/getInfo", true);

            xmlHttpD.onload = function (e) {
                if (xmlHttpD.readyState === 4) {
                    if (xmlHttpD.status === 200) {
                        alert(xmlHttpD.responseText);
                    } else {
                        alert(xmlHttpD.statusText);
                    }
                }
            };
            xmlHttpD.onerror = function (e) {
                console.error(xmlHttpD.statusText);
            };

            var params = "rdverb=INFO&URL=''";
            xmlHttpD.send(params);
        }

推荐答案

取决于参数的类型.看起来像一个URL查询.因此,应该这样做:

Depends on the kind of parameter. Looks like an URL query. Thus this should do it:

string verb = "INFO"
string url = ""
string query = string.Format(http://127.0.0.1:{0}/getInfo?rdverb={1}&URL={2}" , PortNumber, HttpUtility.UrlEncode(url), verb);
xmlHttpD.open("INFO", query, true);


这篇关于如何在XMLHttpRequest中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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