AJAX POST中的URL

The*_*one 5 jquery

我想通过AJAX发布这些变量:

       <div class="vIn" id="star">
        <div id="inner">
         <span id="1" class="disabled"></span>
         <span id="2" class="disabled"></span>
         <span id="3" class="disabled"></span>
         <span id="4" class="disabled"></span>
         <span id="5" class="disabled"></span>
         <input type="hidden" id="<?php echo $_GET['cID']?>" />
        </div>
      </div>
Run Code Online (Sandbox Code Playgroud)

使用此脚本:

$(document).ready(function(){
 $('#inner span').click(function(){
     $(this).prevAll().andSelf().addClass('enabled');  
     var a = $(this).attr("id");
     var cID = $("#inner input").attr("id");
     $.ajax({
           type: "POST",
           url: "ajax/rating.php",
           data: "value=+a+&cID=+cID+",
           success: function(msg){
             alert(data);
           }
         });
     });});
Run Code Online (Sandbox Code Playgroud)

在click事件中,没有警报.我在$ .ajax中使用了正确的数据吗?提前致谢.

T.J*_*der 8

我强烈建议允许jQuery担心正确编码字符串:

var a = $(this).attr("id");
var cID = $("#inner input").attr("id");
$.ajax({
    type: "POST",
    url: "ajax/rating.php",
    data: {value: a, cID: cID},   // <== change is here
    success: function(msg){
        alert(msg);
    }
});
Run Code Online (Sandbox Code Playgroud)

请注意,我传递data$.ajax作为一个对象,而不是作为一个字符串.jQuery会为你正确编码.有关详细信息,请参阅文档.

如果你真的,真的想自己编码,你必须明确地做:

data: "value=" + encodeURIComponent(a) + "&cID=" + encodeURIComponent(cID)
Run Code Online (Sandbox Code Playgroud)

另请注意,我警告msg,不是data,因为这就是你所谓的响应参数success.