...">

AJAX POST URL

I want to publish these variables via 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>

With this script:

$(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);
           }
         });
     });});

There is no warning in the click event. Am I using the correct data in $ .ajax? Thanks in advance.

+5
source share
5 answers

I highly recommend letting jQuery worry about the correct encoding of the string:

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

Note that I pass datain $.ajaxas an object, not as a string. jQuery will correctly encode it for you. See documents for more details .

If you really want to do the encoding yourself, you need to do it explicitly:

data: "value=" + encodeURIComponent(a) + "&cID=" + encodeURIComponent(cID)

Also note that I am warning msg, and not data, like what you called the response argument success.

+6

...

       success: function(msg){
         alert(data);
       }

...

       success: function(data){
         alert(data);
       }

...

data: "value=+a+&cID=+cID+",

, ...

data: "value=" + a + "&cID=" + cID,

, , !

+2

Javascript, data: "value="+a+"&cID="+cID?

0

data: "value=+a+&cID=+cID+" , . URL , , &.

, , , , . .

:

data: { "value": a, "cID": cID }
0

$.ajax

data: "value=+a+&cID=+cID+",

'+' - , , , , . "value =" ..

.

data: "value="+a+"&cID="+cID,
0

All Articles