full URL path in ajax - php

Is it okay to put full URL path in ajax? I'm having problems accessing the url and I'm getting status 0 for my error response.
$.ajax({
url: "http://fullurlpath.com/php/myphppagedata.php",
type: "GET",
data: "somedata="+somedata,
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.responseText);
}
}).error(function(xhr){
alert(xhr.responseText);
alert(xhr.status);
}).done(function(data){
alert(data);
});
Also, inside my http://fullurlpath.com/php/myphppagedata.php I have
header('Access-Control-Allow-Origin: *');

http://en.wikipedia.org/wiki/Same_origin_policy
http://bob.ippoli.to/archives/2005/12/05/remote-json-jsonp/

Instead of requesting it with your client's browser using jQuery, I would make a page on your own domain, for instance call it request.php with:
echo file_get_contents("http://fullurlpath.com/php/myphppagedata.php");
This way your server will request the resource, so that you won't have the same origin policy issues. Then ajax this file instead.
$.ajax({
url: "request.php",
...
You can also use cURL instead of file_get_contents() for more elaborate functionality.

Related

jQuery ajax POST request to apache PHP server not receiving data

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.

PHP files not loading on ajax calls (AWS server)

The following error is being thrown in the browser console while ajax calls are made in the AWS server
XMLHttpRequest cannot load http://<awsdns>/folder/webpage.php.
Origin http://awsdns is not allowed by Access-Control-Allow-Origin.
Here is how ajax call is being made
$.ajax({
type : 'POST',
url : gbWebroot+'folder/webpage.php',
data : postFormData,
dataType: 'json',
encode : true,
success : function(data) {
}
});
Any idea why this error is being thrown?

Error in CORS ajax request

In php i do echo json_encode($dump);
If echo it out using php i get {"load":"0.64 0.58 0.52 2\/361 12978\n","procs":"8\n"}
Than i make CORS Request using dataType:jsonp
$(function () {
$.ajax({
type: "POST",
ContentType: 'application/json; charset=utf-8',
url: 'http://labs.isaumya.com/loadtest/load',
dataType: "jsonp",
success: function (response) {
console.log(response.data);
},
error: function (xhr, status, error) {
console.log(error);
}
});
});
I get this error on the console:
DEMO
You are dealing with JSON, not JSONP. dataType: "jsonp", should be dataType: "json",
You can remove the data parameter entirely if your server outputs the correct content-type header for JSON (application/json).
JSONP is a hack to work around the Same Origin Policy from before CORS was designed and implemented by browsers. CORS is the modern approach to making cross origin requests. You use it instead of JSONP.
Both CORS and JSONP are technologies that must be supported by the server. http://labs.isaumya.com/loadtest/load doesn't appear to support either. You will have to modify the server if you want it to supply data in JSONP format or grant permission with CORS.
Unrelated to your actual problem:
You have no data parameter so you aren't sending JSON to to the server, remove the ContentType parameter. Since you aren't POSTing any data, you should probably be making a GET request, not a POST request.

cross domain ajax call + php

I want to get data from the webservices through jquery ajax call(cross domain). After fetching data from webservices, i need to show it as a dataTable using php.
Can anyone help me regarding this or just give me some sampe examples.
my ajax function is as follows:
$.ajax({
type: "POST",
url:"my webservice url",
//data: json,
//contentType: "application/json; charset=utf-8",
crossDomain: true,
dataType: 'json',
async:false,
success: function(data, textStatus, jqXHR)
{
alert("Download success");
alert(data);
},
error : function(jqXHR, exception)
{
alert(jqXHR.status);
}
});
$.ajax({
url:"yourPageName.php",
dataType: 'jsonp', // N.B! JSONP It is lower Case OK?
success:function(json){
// json (an Array)
alert("Success");
},
error:function(){
alert("Error");
},
});
For more info please visit here http://api.jquery.com/jQuery.ajax/
Jsonp is better way to do it. But if you really do with json you can add
header("Access-Control-Allow-Origin: *");
to your php code. This way your server will response any request and domain. You can customize
"*" to accept domain.
But be aware this will cause security issue.

jquery ajax post to non-ssl page while current page is ssl

Ok, situation:
an https / ssl page
jquery
a form
submitted via ajax to a non-ssl pagge
getting no usefull response
the same scenario, non-ssl to non-ssl works perfect.
I can view my console, but cant get any usefull info from it why the request fails...
$.ajax({
type: "POST",
url: form.attr("action"),
data: form.serialize(),
error: function(res){ console.log(res) },
notmodified: function(res){ console.log(res) },
parsererror: function(res){ console.log(res) },
timeout: function(res){ console.log(res) },
success: function(res){ alert('succes!'); }
});
You can't make AJAX calls from non-SSL page to a SSL URL. This violates the SOP (Same Origin Policy) because the protocols (HTTP vs HTTPS) are different. Some old browsers don't have this restrictions but all new ones enforce this now.
Read this article for more details,
http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_SOP

Categories