I have made web service in php, which sends email to seller with information which is sent in parameters. If we are sending those information in GET, the web service works well. But if we send that information in POST, the web service (php script) shows nothing.
Here is url of that web service :
http://demo1.zenithtechnosol.net/carsGuide/contactSeller.php?seller_id=0&name=Anjum&email=abc#ccc.com&mobile=00923344239490&area=Dubai&message=This%20is%20test%20message.
Currently i am just showing param passed using
print_r($_REQUEST);
Well this is working fine because i am sending those paramerters in GET but I am trying to send those parameters in POST using chrome extension "Simple REST client", I am getting nothing.
I guess, I need to set headers in my script, but not sure about that. Or when calling that web service we need to set any thing header in request.
Here is how request via POST is send :
Ext.Ajax.request({
url: this.getBaseUrl() + webServiceUrl,
timeout: 240000,
method: httpMethod,
disableCaching: false,
useDefaultXhrHeader: false,
jsonData : {
"seller_id":seller_id,
"name":name,
"email":email,
"mobile":mobile,
"area":area,
"message":message },
scope: me,
success: function(response) {
Ext.Viewport.unmask();
successCallBack(response);
},
failure: function(response) {
Ext.Viewport.unmask();
failureCallback(response);
}
});
Any help would be highly appreciated..
Thanks..
Anjum
Try putting the jsonData in params as follows :
Ext.Ajax.request({
url: this.getBaseUrl() + webServiceUrl,
timeout: 240000,
method: httpMethod,
disableCaching: false,
useDefaultXhrHeader: false,
params: {
jsonData : {
"seller_id":seller_id,
"name":name,
"email":email,
"mobile":mobile,
"area":area,
"message":message },
}},
success: function(response){
var text = response.responseText;
// process server response here
}
});
Related
I have the following doubt, I am sending an ajax request to laravel but one of the parameters has a lot of information
but in laravel using $request->all() I get an empty array, but when I send only a part of the text (approximately a quarter) the data arrives to laravel, my question is, is there a limit on post requests? If so, how could I correct this? attached ajax request code
$.ajax({
type: 'POST',
url: _url,
data: _data,
success : function(response){
console.log("sucess")
},
error: function(response){
console.log("ajax error" + JSON.stringify(response))
},
});
data I am sending including the token.
This js jQuery script is properly formatting the form data to an object and sending it to my PHP code.
$("#formSignup").submit(function(e){
// Map the array into a properly formatted object
var data = {};
$.map($(this).serializeArray(),function(n, i){
data[n.name] = n.value;
});
// Send the http request
$.ajax({
type: "POST",
url: "api/signup.php",
data: JSON.stringify(data),
contentType: "application/json",
success: function(response){
//console.log(JSON.parse(response));
console.log(response);
},
failure: function(err){
console.error(err);
}
});
e.preventDefault(); // Don't submit defaultly
});
However PHP is unable to receiving the data. print_r($_POST) will print an empty array and the values are not accessible. It's not getting a response from the wrong file either.
That is pretty weird considering that the XmlHttpRequest recorded by the mozilla dev console is clearly posting the JSON data.
Other questions have been answered by stating redirects made by the server. However I haven't done anything at all with these settings and I got post requests working on the same server with my own function a while back.
Remove contentType property from your ajax object.
To serialize data just use $(this).serialize().
$("#formSignup").submit(function(e){
var data = $(this).serialize();
// Send the http request
$.ajax({
type: "POST",
url: "api/signup.php",
data: data,
success: function(response){
console.log(response);
},
failure: function(err){
console.error(err);
}
});
e.preventDefault(); // Don't submit defaultly
});
After that you should be able to successfully see your data inside $_POST variable.
I notice my get-data isn't passed through in my GET request. I've stripped it down to:
$http({
url: "http://www.myurl.com/somefolder/demo.php",
method: "GET",
data: { info: "lala"},
timeout: 5000
})
.then(function (res) {
var data = res.data;
console.log((data));
},function (error) {
alert("Something went wrong");
})
demo.php only contains:
<?php var_dump($_GET) ?>
It does work when I visit the URL with my browser, but via the http-get, everything seems fine but the data is never past.
console.log(data) always returns array(0) {} as if I didn't send any data.
Don't know if this is relevant, but I'm using this in an Angular based Ionic-app, and I'm testing it in my browser.
It's puzzling me for hours now...
In the configuration object, data represents the body of the request (Used by a POST request for example). It seems that you want to add parameters to the URL. You should use params key instead.
$http({
url: "http://www.myurl.com/somefolder/demo.php",
method: "GET",
params: { info: "lala"},
timeout: 5000
})
More details here: https://docs.angularjs.org/api/ng/service/$http#usage
I have a PHP service at api/add in my application, and you can pass it rq and name via POST. rq tells the service what function to perform, so in my example addCity is defined, and it inserts a city into the database, and name is the name of the city.
So, with that being said, here is my angular code. I'm defining an angular module above with ngRoute.
whereApp.controller('AddCityCtrl', function($scope, $http) {
$scope.addCity = function() {
$http({
method: "POST",
url: '/api/add/',
data: { rq:'addCity', name: $scope.name },
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).success(function (data, status, headers, config) {
console.log(data);
});
/*
$.ajax({
url: "/api/add/",
type: "POST",
data: { rq: 'addCity', name: $scope.name },
dataType: "json"
})
.success(function(data) {
console.log(data);
});
*/
}
});
Here's the problem. The ajax request (jQuery style that is commented out) works. I'm wanting to use the angular style since, well, it's what I'm using and what I'm trying to learn a little bit more about. The jQuery ajax call gives me back the success message that I have from server-side, and the $http method says that the rq variable is not defined, which I'm accessing via $_POST['rq'].
I've done some research already on Google, but so far have only come up with adding headers: {'Content-Type': 'application/x-www-form-urlencoded'} like this post says.
Can anyone tell me what the difference is between these two ajax calls (or if there is something else I haven't considered)?
Since it is json data that is being sent in php you cant get it by simple $_POST you need to do this kind of stuff to get this posted data
$data=file_get_contents('php://input');
$dataobj= json_decode($data);
Here you get data first then decode it from json to normal object
I'm trying to get the SalesForce Rest API to work and I've debugged my problem down to this point:
When I make an AJAX call from my web app to my back-end (which is on a different domain than the backend), all of the AJAX headers end up crammed into $_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'] and there is no way for me to get access to their values.
return $j.ajax({
type: 'POST',
url: my_endpoint_url,
cache: false,
processData: false,
data: 'grant_type=refresh_token&client_id=' + this.clientId + '&refresh_token=' + this.refreshToken,
success: callback ,
error: error ,
dataType: "json",
beforeSend: function(xhr) {
if (that.proxyUrl !== null) {
xhr.setRequestHeader('SalesforceProxy-Endpoint', url);
}
}
});
On the server side, I only receive:
[HTTP_ACCESS_CONTROL_REQUEST_HEADERS] => accept, salesforceproxy-endpoint, doiwork, origin, content-type
How can I access the value of "salesforceproxy-endpoint" over on the server side? Likewise, I can't seem to find the "data" of the ajax call anywhere..
UPDATE: Just for giggles I moved my end-point to the same domain. Now it is working as expected.
[HTTP_SALESFORCEPROXY_ENDPOINT] => https://login.salesforce.com//services/oauth2/token
Is there any way to get this working cross domain?
You just got aware what the Same origin policy is ;)