jQuery AJAX pass url as string

前端 未结 2 1998
南方客
南方客 2021-01-15 03:59

I have a ajax function that is passing a string of variables to my script but I have one variable that needs to contain a full url with parameters.

What happens is t

相关标签:
2条回答
  • 2021-01-15 04:04

    Don't try to build your form data by hand. jQuery will encode it for you (with appropriate escaping) if you pass it an object.

    var url = "http://domain.com/index.php?var1=blah&var2=blah";
    
    $.ajax({
        type: "POST",
        url: "/scripts/error_check.php",
        data: { url: url, rooftop_id: rooftop_id },
        dataType: 'json'
    }); 
    
    0 讨论(0)
  • 2021-01-15 04:22

    Use encodeURIComponent() on url variable:

    var url = "http://domain.com/index.php?var1=blah&var2=blah";
    
    var dataArray = "rooftop_id=1&url=" +encodeURIComponent(url);
    
    $.ajax({
        type: "POST",
        url: "/scripts/error_check.php",
        data: dataArray,
        dataType: 'json'
    }); 
    
    0 讨论(0)
提交回复
热议问题