Ajax GET works but POST not working - ajax

I make a request with Ajax and the request works fine when I use GET. It doesn't if I use POST in my script and my Rest-Service.
$.ajax({
type: 'POST',
url: 'http://localhost/WebRestService/rest/User/deletePC',
dataType: "text",
success: function(antwort){
alert("response:"+antwort);
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(antwort,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(antwort);
}
},
error:function(x,e){
if(x.status==0){
alert('You are offline!!\n Please Check Your Network.');
}else if(x.status==404){
alert('Requested URL not found.');
}else if(x.status==500){
alert('Internel Server Error.');
}else if(e=='parsererror'){
alert('Error.\nParsing JSON Request failed.');
}else if(e=='timeout'){
alert('Request Time out.');
}else {
alert('Unknow Error.\n'+x.responseText);
}
}
});
If I use POST I get an unknown error. What is the problem?
THX!
Edit: DELETE and PUt also doesn't work.
Edit2: if I use firefox Poster it works fine.

My where is you data that you are sending in your ajax request?
When you send post request you have to identify your data that you are sending.
You are missing data attribute of post request.
$.ajax({
type: 'POST',
url: 'http://localhost/WebRestService/rest/User/deletePC',
data:'some data',
dataType: "text",
success: function(antwort){
alert("response:"+antwort);
},
});
For further reading consult here

Related

WordPress - Get server time using ajax and admin-ajax.php

I am new to using Ajax to get WordPress data.
The following code should return the server time but the response always is "400 Bad Request".
$.ajax({
url: obj + "?action=wps_get_time&format=U",
success: function(data) {
console.log(data);
}
});
Also tried it as POST and it was the same.
$.ajax({
url: obj,
method: "post",
data: { action: "wps_get_time", format: "U" },
success: function(data) {
console.log(data);
}
});
Any suggestions what's wrong please? Can't figure.
I always thought there are actions I can use always such as wps_get_time, without using a plugin. Am I wrong?
Ist there any easy way to get the server time by ajax?
Thank you all in advance.
The code below will return server time in Indochina and log it to console.
$.ajax({
type: 'GET',
cache: false,
url: location.href,
complete: function (req, textStatus) {
var dateString = req.getResponseHeader('Date');
if (dateString.indexOf('GMT') === -1) {
dateString += ' GMT';
}
var date = new Date(dateString);
console.log(date);
}
});```

Ajax call to another website

For some reason when I run this simple javascript snippet I get the message "Error! Status = 404 Message = error"
callPHP(1)
function callPHP(args)
{
$.ajax({
url: "http://lectiogroupgenerator.esy.es/index.php",
type: 'post',
data: { "json" : JSON.stringify(args) },
dataType: "json",
success: function (data)
{
if (data)
{
alert(data);
return data;
}
else
{
alert("Data is empty");
}
},
error: function (xhr)
{
alert('Error! Status = ' + xhr.status + " Message = " + xhr.statusText);
}
});
return false;
}
My PHP file is just:
<?php
?>
I'm guessing 404 implicates that the php could not be found, but it does exist and I have no clue why it can't find it maybe it has something to do with me making a google chrome extension?
It might be because of the CORS issue. http://lectiogroupgenerator.esy.es/index.php doesn't allow cross origin HTTP requests.
If that's not the case try explicitly defining the website in the permissions in the manifest file to allow requests in and out to that website.
The problem was caused by the Same-origin policy, it was solved when I got an SSL certificate for my website.

Post this $ajax request to REST API

I have this ajax POST code, which works directly on browser, but when i want to make a POST using the same data on Fiddler or SoapUI or using httpClient or HttpWebPost it doesn't work, always returns ERRROR. Below are the Ajax code and Fiddler. Thank you in advance.
$.ajax({
type: "POST",
url: "http://IP/address/post/something",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType: "text",
data:
{ message: 'testMessage', destination: '8888888888', campaign: 'UATTest', agent: 'TestingAgent'}
,
success: function (resp) {
if (resp != "ERROR") {
} else {
}
},
error: function (e) {
}
});
Update :
Does anybody know how to consume a REST API using FormParam ? how is it differ from JSON and XML ?
params:
#FormParam("destination"), #FormParam("message"), #FormParam("campaign"), #FormParam("agent")

Send ajax request is not working

I send following curl request to get result from mongodb.
curl --data 'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}' 'http://82.196.xxx.xxx:27080/weather/_cmd'
It is working. I try to send ajax request to do above curl request.
$.ajax({
url: 'http://82.196.xxx.xxx:27080/weather/_cmd',
type: 'POST',
// dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
data: {"geoNear" : "items", "near":[6.8590845,79.9800719]},
})
.done(function() {
console.log("success");
})
.fail(function() {
console.log("error");
})
.always(function() {
console.log("complete");
});
I think issue is my data object. curl request has 'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}. But i don't know how it convert to ajax request.
But it is not working. I got 200 OK status. Please help me.
$.ajax({
url: '/weather/_cmd',
type:'POST',
dataType: 'json',
crossDomain : true,
data: {
cmd: JSON.stringify({
"geoNear" : "items",
"near": [6.8590845,79.9800719]
})
},
success: function(res) {
//do stuff with res
}
});
ProxyPass /weather http://82.196.11.207:27080/weather/_cmd
ProxyPassReverse /weather http://82.196.11.207:27080/weather/_cmd
Add above Proxy pass to your Apache and try this. it will work. problem is you cant pass POST Request by using jsonp. mongo we need POST requests. this is one way to do it. :D i tested it and works perfectly for me.
-update - it will not accept json and you have to pass it as string.

Ajax Call with PUT method

i am trying to make ajax call with PUT method. Below is the code, but i am getting with the error XML Parsing Error: no element found Location: moz-nullprincipal:{c847a4af-f009-4907-a103-50874fcbbe35} Line Number 1, Column 1:
$.ajax({
type: "PUT",
async: true,
url: "http://localhost:8080/karthick/update",
data: JSON.stringify(params),
contentType: "application/json",
dataType: "JSON",
processdata: true,
success: function (json) { //On Successfull service call
},
error: function (xhr) {
alert(xhr.responseText);
}
});
return false;
};
function ServiceFailed(xhr) {
alert(xhr.responseText);
if (xhr.responseText) {
var err = xhr.responseText;
if (err)
error(err);
else
error({ Message: "Unknown server error." })
}
return;
}
But this service is working Good with Rest-client jar. Also my POST method works fine in my browser. Please help me in this.
Regards
Karthick
Usually, this error comes, when making a cross browser request. Try data: JSONP and see if it helps.

Resources