How does POST and PUT works? [duplicate] - codeigniter

This question already has answers here:
What is the difference between POST and GET? [duplicate]
(7 answers)
Closed 9 years ago.
I know that there are four request types such as get ,put ,post ,delete .When and why will I use type='put' or type="post" ?Basically what are the differences between them?
$.ajax({
url: '<?php echo site_url('rest_api / contacts ')."?format=json"; ?>',
type: "put",
data: $('#subpanel-contacts-add-form').serialize(),
success: function (response) {
//some tasks
}
error: function () {
$("#subpanel-contacts-form-result").html('<div class="alert alert-error">Error: There was an error while submitting!</div>');
}
});

PUT and GET are the protocols to exchange data between the server and UI define by the standard committee.
Type attribute in the $.ajax function in the way to tell the engine that what kind of request is being
generated and accordingly it is handled on the server side.
Refer to the link posted below for more explanation and difference between the protocols.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

Related

Why the view function is returning 'None'? [duplicate]

This question already has answers here:
Get the data received in a Flask request
(23 answers)
Closed 5 years ago.
I'm trying to send a simple POST request containing some data. I'm processing the request in flask and trying to return the same data. But it's always returning 'None'.
AJAX
$(function(){
$('.btn').click(function(){
var roll_no = $(this).parent().parent().children(':first-child').html();
$.ajax({
url: '/delete_student',
data: {id: roll_no},
type: 'POST',
dataType: "text",
success: function(response){
console.log(response);
},
error: function(error){
console.log(error);
}
});
});
});
FLASK
#app.route('/delete_student',methods=['GET','POST'])
def delete_student():
if request.method=='POST':
roll_no = request.args.get("id")
return str(roll_no)
request.args contains the URL parameters for GET requests. Since you're using a POST request, it will default to None. You should instead use request.form as in:
if request.method=='POST':
roll_no = request.form['id']
return str(roll_no)
See the Flask documentation for more information on the request object.

Method in Ajax other than POST and GET

I was asked in an interview what are the methods in AJAX other than GET and POST. I googled but I couldn't find any . Could someone please tell me.
HEAD Same as GET but returns only HTTP headers and no document body
PUT Uploads a representation of the specified URI
DELETE Deletes the specified resource
OPTIONS Returns the HTTP methods that the server supports
These are generally used methods
Also see detail about
There are four types of calls you cand do to a REST API GET,POST,PUT and DELETE. PUT should be suposedly used to modify already existing data while DELETE should be used to eliminate data. Still due to convention, PUT and DELETE are barely used. Here are a couple of examples of PUT and DELETE Ajax calls
$.ajax({
url: urlCalltoDeleteResource),
type: 'DELETE',
success: function(data) {
alert('Deleted');
},
error: function(data) {
alert('Not Deleted');
},
});
$.ajax({
url: urlToUpdate,
type: 'PUT',
data: "name=NameToUpdate",
success: function(data) {
alert('Load was performed.');
}
});

How to pass the ajax post request from one server to another server using jquery with php? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass data from jQuery to PHP for an ajax post
How do i pass the values from one server to another server using ajax,jquery
$.ajax({
url:'/url/on/other_server',
type: 'POST',
contentType:"application/x-www-form-urlencoded",
data: '{"key":"value"}',
success: function(data, status) {
console.log(status);
},
error: function(xhr, desc, error) {
console.log(error);
}
});
Ref: http://api.jquery.com/jQuery.ajax/

MVC3 Controller returning JsonFile [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
I am having a problem with a json result. When calling from the jquery it is returning a file to be saved instead of executing the success function. The get jquery request occurs in the document.ready function.
Any help would be appreciated.
public ActionResult Locations()
{
LocationsModel lm = new LocationsModel();
return Json(lm.getPins(), JsonRequestBehavior.AllowGet);
}
I have also tried:
public JsonResult Locations()
{
LocationsModel lm = new LocationsModel();
return Json(lm.getPins(), JsonRequestBehavior.AllowGet);
}
The jquery is as follows:
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: this.href,
data: "{}",
dataType: "json",
success: function (msg) { getPins_success(msg); },
error: OnError
});
Thanks,
Chris
Edit:
Never mind it was a duh. Once I moved the json request to another action in the controller and loaded the view it all worked out. Now I am having parsing problems but that is another issue all together.
you should use getJson instead.
For you it would be:
$.getJSON(this.href, function (msg) { getPins_success(msg); });
This will let you parse the return data as json.

How to set request header strings [duplicate]

This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 7 years ago.
I have a RESTful service on a secured site. I can build a request just fine with the REST client in Firefox, but when I try to build it in code I get a 401. Here is my code
(function() {
$.ajax({
url : "https://myrestsite",
type : "GET",
//beforeSend: function (req){
// req.setRequestHeader("Authorization","myauthstring");
// req.setRequestHeader("Content-Type", "application/json");
//},
headers : {
"Authorization" : "myauthstring",
"Content-Type" : "application/json",
},
success : function (){alert('we got here')}
});
})();
This shows up in web developer tools in FF as:
Request-Headers:authorization,content-type
When what I need (from the rest client is FF) is:
Authorization:myAuthstring
Content-Type:application/json
Any clues as to what I am doing wrong?
**edit - turns out I am hitting the cross domain restriction. How does the rest client get around that?
you can pass contentType as an option to $.ajax
$.ajax({
...
contentType: 'application/json'
});

Resources