Reputation:
when i send ajax request i got the error that
$.ajax({
type: "POST",
url: furl2,
cache: false,
contentType: "application/json;charset=utf-8",
data: { id: id, url: $("#id" + id + " .customURL").val() },
success: function (msg) {
//some code here
},
});
}
i got the error that id is undefined even i put them.
how i can send a ajax request to server using ajax.
Upvotes: 0
Views: 1040
Reputation: 1337
// create an empty object
var data = {};
// create and populate nodes in the object
data.id = id;
data.url = //what ever you wanted it to be
$.ajax({
type: "POST",
url: furl2,
cache: false,
dataType: "json",
data: data, // send the object to the server as post data
success: function (result) {
// try and grab a node in the json object
console.log(result.foo);
},
});
This will send the data as posts to the server, and require json encoded data in return, which is nice becasue you don't have to send the html from the server, you can just construct it in the js.
If you wanna encode data from the server with for example php you can do it on a regular array or object
<?php
// fetch what ever post we need
$id = $_POST['id'];
$arr = array(
"foo" => "1",
"two" => "2"
);
return json_encode($arr);
?>
Upvotes: 1
Reputation: 2778
In JSON it's
String : value
Try adding quotes around your field names, e.g.
{
"type": "POST",
"url": furl2,
"something": "else",
"fortytwo": 42
}
Edit: There's a comma after your success function that seems to do nothing. Have you checked that?
Upvotes: 0