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!

dongsi2317
dongsi2317 你应该在PHP方面这样做,或者只是使用GET;)
大约 6 年之前 回复
dongliuliu0385
dongliuliu0385 谢谢!我对我的测试代码有引用但是单独传递变量。
大约 6 年之前 回复
dpt8910
dpt8910 url:MISSINGQUOTEHERE/scripts/error_check.php“,
大约 6 年之前 回复
dongyou2305
dongyou2305 它还缺少引号?你有没有像这样尝试jsfiddle.net/6ZUzh/1
大约 6 年之前 回复
dongwei1234
dongwei1234 我更新了我的代码
大约 6 年之前 回复
drrog9853
drrog9853 缺少报价!
大约 6 年之前 回复

2个回答

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

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'
}); 
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐