javascript variables to a server through ajax problem
My goal is to send data to the server through Ajax.
But the server doesn't receive data exactly.
Here is the code I wrote.
in javascript
var a = 1, b = 2, c = 3, d = 4, e = 5;
var f = { 'h':11, 'i':22, 'j':33};
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: { 'a':a, 'b':b, 'c':c, 'd': d, 'e': e,'dataImportant': f},
success: function(result){
/* do something */
开发者_JAVA技巧 },
error: function(result){
/* do something */
}
});
in php
function getAjax(){
$a = $this->input->post("a");
$b = $this->input->post("b");
$c = $this->input->post("c");
$d = $this->input->post("d");
$e = $this->input->post("e");
$f = $this->input->post("dataImportant");
echo "<pre>";
print_r($f);
echo "</pre>";
return;
}
result:
<pre></pre>
I intended to get the data in the form of an array.
but it could not be reached well
what is my mistake?
Are a
, b
, c
, d
, etc... in your javascript defined variables? If not, and you are just palying:
var f = { h:'11', i:'22', j:'33'};
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: { 'a':'a', 'b':'b', 'c':'c', 'd':'d', 'e':'e','f':'f' },
success: function(result){
/* do something */
},
error: function(result){
/* do something */
}
});
I think you don't need the quotes around your data declaration:
data: { a:a, b:b, c:c, d:d, e:e, f:f},
The first letter defines what you are naming the value that follows.
{name:value, name:value}
You would only need the quotes around the value if you are defining it in the data declaration
data: {a:'a',b:'b',etc}
Try my first example and see if your php script can echo the sent values.
精彩评论