Reputation: 2980
I am trying to parse server data to a REST API as post to be stored in the database. But the data doesn't seem to be get parsed. If I hard code the url as follows the data will get posted successfully,
$.ajax({
url: 'index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1',
uccess: function(data) {
alert(data);
},
type: "post"
});
But if I try to dynamically insert the data as follows, it doesn't work:
$.ajax({
url: 'index.php/rest/resource/questions/',
data: { 'uId\/':qUserId, '\/qTitle\/':qTitle, '\/qBody\/':qBody, '\/qTag\/':'1' },
success: function(data) {
alert(data);
},
type: "post"
});
This question might have a simple solution but I could not get any positive results from the resources I found online.
EDIT
When I paste the query manually with hard coded data on the browser as follows,
index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1
T submits the data to be stored in the database. My requirement is to make the variables dynamically inserted to the url via variables in the ajax call :)
EDIT 2
Rest Controller Code
<?php
class Rest extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('student');
$this->load->helper('url');
}
// we'll explain this in a couple of slides time
public function _remap()
{
// first work out which request method is being used
$request_method = $this->input->server('REQUEST_METHOD');
switch (strtolower($request_method)) {
case 'post' : $this->doPost(); break;
default:
show_error('Unsupported method',404); // CI function for 404 errors
break;
}
}
public function doPost(){
$args = $this->uri->uri_to_assoc(2);
switch ($args['resource']) {
case 'questions' :
$res = $this->student->askQ($args);
if ($res === false) {
echo json_encode(array('error' => 'unable to post','status' => 1));
}
else {
echo json_encode(array('status' => 0));
}
break;
default:
show_error('Unsupported resource',404);
}
echo 'posted';
}
}
?>
Student Model which is utilized from Rest Controller
<?php
class Student extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function askQ($args)
{
$timeVal = date( "Y-m-d H:i:s", mktime(0, 0, 0));
$qVotes = 0;
$qStatus = 1;
if (!isset($args['uId']) || !isset($args['qTitle']) || !isset($args['qBody']) || !isset($args['qTag'])) {
return false;
}
$this->db->insert('questions',array('userId' => $args['uId'],'questionTitle' => $args['qTitle'],'questionBody' => $args['qBody'],'tagQuestionId' => $args['qTag'],'postDate' => $timeVal,'status' => $qStatus,'votes' => $qVotes));
return true;
}
}
?>
Upvotes: 2
Views: 5910
Reputation: 441
You should tell something about your REST-server too. Your examples produce clearly different HTTP requests and I think your server code does not handle both:
Your 1st example sends this kind of HTTP Request:
POST /index.php/rest/resource/questions/uId/1/qTitle/TestQ/qBody/TestBody/qTag/1 HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 0
No content, only target url.
Your 2nd example sends this kind of HTTP Request:
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 68
uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1
Your target url is different and then the data is in request body.
If you change ajax method to GET, then jQuery adds parameters to url but its still not equivalent of your hard coded request:
GET /index.php/rest/resource/questions/?uId%2F=123&%2FqTitle%2F=some+title&%2FqBody%2F=body+txt&%2FqTag%2F=1 HTTP/1.1
Host: some.server.invalid
Update
So, if requirement is to make your first request dynamic, you have to build target url manually:
$.ajax({
url: 'index.php/rest/resource/questions/uId/'+qUserId
+'/qTitle/'+encodeURIComponent(qTitle)
+'/qBody/'+encodeURIComponent(qBody)+'/qTag/1',
success: function(data) {
alert(data);
},
type: "post"
});
Though, it might be worth of it to consider if you should change your server code. If you are sending blog post or something like that, you should not put it in the url. You should send it as proper POST request. So, I would design the REST API so that it accepts this request:
$.ajax({
url: 'index.php/rest/resource/questions/',
data: { 'uId':qUserId, 'qTitle':qTitle, 'qBody':qBody, 'qTag':'1' },
success: function(data) {
alert(data);
},
type: "post"
});
That example sends this kind of HTTP Request:
POST /index.php/rest/resource/questions/ HTTP/1.1
Host: some.server.invalid
Content-Length: 47
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
uId=123&qTitle=some+title&qBody=body+txt&qTag=1
Upvotes: 1
Reputation: 1656
When you use data
of $.ajax
, the result is added as form data (because you use type: "post"
).
Judging from your question, it seems you want:
$.ajax({
url: 'index.php/rest/resource/questions/uId/'+qUserId+'/qTitle/'+qTitle+'/qBody/'+qBody+'/qTag/1',
success: function(data) {
alert(data);
},
type: "post"
});
I should point out, however, that using data
is a better option for POST
-calls if you're sending a lot of data and then just look through the POST
data on the server (e.g. $_POST['qBody']
in PHP)
Upvotes: 0
Reputation: 3855
You can do like following by encoding parameters
var data = 'uId/='+qUserId+'&/qTitle/='+qTitle+'&/qBody/='+qBody+'&/qTag/=1';
data = encodeURI(data);
$.ajax({
type: "post",
url: 'index.php/rest/resource/questions/',
data: data,
success: function(data) {
alert(data);
}
});
Upvotes: 0