How do I pass along variables with XMLHTTPRequest

后端 未结 7 794
孤街浪徒
孤街浪徒 2020-11-30 03:47

How do I send variables to the server with XMLHTTPRequest? Would I just add them to the end of the URL of the GET request, like ?variable1=?v

相关标签:
7条回答
  • 2020-11-30 04:06

    Manually formatting the query string is fine for simple situations. But it can become tedious when there are many parameters.

    You could write a simple utility function that handles building the query formatting for you.

    function formatParams( params ){
      return "?" + Object
            .keys(params)
            .map(function(key){
              return key+"="+encodeURIComponent(params[key])
            })
            .join("&")
    }
    

    And you would use it this way to build a request.

    var endpoint = "https://api.example.com/endpoint"
    var params = {
      a: 1, 
      b: 2,
      c: 3
    }
    
    var url = endpoint + formatParams(params)
    //=> "https://api.example.com/endpoint?a=1&b=2&c=3"
    

    There are many utility functions available for manipulating URL's. If you have JQuery in your project you could give http://api.jquery.com/jquery.param/ a try.

    It is similar to the above example function, but handles recursively serializing nested objects and arrays.

    0 讨论(0)
  • 2020-11-30 04:09

    If you're allergic to string concatenation and don't need IE compatibility, you can use URL and URLSearchParams:

    const target = new URL('https://example.com/endpoint');
    const params = new URLSearchParams();
    params.set('var1', 'foo');
    params.set('var2', 'bar');
    target.search = params.toString();
    
    console.log(target);

    Or to convert an entire object's worth of parameters:

    const paramsObject = {
      var1: 'foo',
      var2: 'bar'
    };
    
    const target = new URL('https://example.com/endpoint');
    target.search = new URLSearchParams(paramsObject).toString();
    
    console.log(target);

    0 讨论(0)
  • 2020-11-30 04:13

    Following is correct way:

    xmlhttp.open("GET","getuser.php?fname="+abc ,true);
    
    0 讨论(0)
  • 2020-11-30 04:13

    Yes that's the correct method to do it with a GET request.

    However, please remember that multiple query string parameters should be separated with &

    eg. ?variable1=value1&variable2=value2

    0 讨论(0)
  • 2020-11-30 04:14

    The correct format for passing variables in a GET request is

    ?variable1=value1&variable2=value2&variable3=value3...
                     ^ ---notice &--- ^
    

    But essentially, you have the right idea.

    0 讨论(0)
  • 2020-11-30 04:15

    If you want to pass variables to the server using GET that would be the way yes. Remember to escape (urlencode) them properly!

    It is also possible to use POST, if you dont want your variables to be visible.

    A complete sample would be:

    var url = "bla.php";
    var params = "somevariable=somevalue&anothervariable=anothervalue";
    var http = new XMLHttpRequest();
    
    http.open("GET", url+"?"+params, true);
    http.onreadystatechange = function()
    {
        if(http.readyState == 4 && http.status == 200) {
            alert(http.responseText);
        }
    }
    http.send(null);
    

    To test this, (using PHP) you could var_dump $_GET to see what you retrieve.

    0 讨论(0)
提交回复
热议问题