drlnsli18864734 2014-05-23 19:57
浏览 146

jQuery AJAX将url作为字符串传递

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 that var1 and var2 become $_POST variables but I need to save the whole url variable as a string.

var url = "http://domain.com/index.php?var1=blah&var2=blah";

var dataArray = "rooftop_id=" +rooftop_id+ "&url=" +url;

        $.ajax({
            type: "POST",
            url: "/scripts/error_check.php",
            data: dataArray,
            dataType: 'json'
        }); 

I would like my $_POST variable to look like this:

$_POST['rooftop_id'] would be '1234'
$_POST['url'] would be 'http://domain.com/index.php?var1=blah&var2=blah'

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • donglao7947 2014-05-23 20:36
    关注

    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'
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题