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.
Related
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 am using an API where I can put my script on the 3rd party checkout page (https). I need to fetch the details that user is entering on checkout page.
I placed a javascript file and using jsonp ajax request on click of 'Order' button, I am sending the user form information in serialize form to my own server (http).
$(document).ready(function(){
$('.place_order.btn').click(function(){
var formData = $('form').serializeArray();
$.ajax({
type: 'POST',
url: 'http://myserver-script/test.php',
crossDomain: true,
data: formData,
dataType: 'jsonp',
success: function(responseData, textStatus, jqXHR) {
alert('POST Success');
},
error: function (responseData, textStatus, errorThrown) {
alert('POST failed.');
}
});
})
});
What are the steps that I need to follow to make information transfer secure?
Thanks
I need to access a php file from another server
i.e, the server which I have doesn't support php.I need to send email from this.
I tried cross domain a server which has php and php function to send email.
I tried this using Jsonp
This is my code
var app = 'http://www.maildomain.com/mail.php';
$.ajax({
url: app,
async: true,
dataType: "jsonp",
jsonp: "jsoncallback",
type:"POST",
success: function(html){
alert("aa");
},
error: function(){
}
});
Disable same origin policy in Chrome
Go to this link
It should work after you done this
It's google chrome that is doing cross domain issue
Thanks for the answers given.
Everybody was close to the answer
I got it anyway... it was an asynchronous parameter which was causing problem. It needed to be set false.
This worked
var app = 'http://www.maildomain.com/mail.php';
$.ajax({
url: app,
async: false,
dataType: "jsonp",
jsonp: "jsoncallback",
type:"POST",
success: function(html){
alert("aa");
},
error: function(){
}
});
I need a jsonp response from cross domain url. My jsonp code is here
$.ajax({
dataType: 'jsonp',
async: false,
contentType: "application/json",
jsonpCallback: "domaincheck",
data:{
id:k,
domain:l
},
url: 'http://example.com/param',
success: function (data) {
console.log(JSON.stringify(data));
},
In the php file, the output is like this:
<?php
echo $_GET['callback']."([Correct])";
?>
I knew that the data what I am returning back is incorrect. Because I think we should pass response as json data only. But in this case, sending only "correct" as response, how should we set it, so that we get the response correctly.
The error what I get in console is this:
Uncaught SyntaxError: Unexpected identifier
I have created Restful web service in jomsocial environment.
It's on the local machine.
When I test it using REST console it return's response.
local url formed is...
http://localhost:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c
Problem:
When I call same using jQuery.ajax, always error callback function is getting called.
Ajax call is...
$.ajax({
type: "POST",
url: "http://localhost:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c",
data: "{ username: 'sai.kiks2#gmail.com', password: '123456'}",
contentType: "application/json; charset=utf-8",
cache : false,
dataType: "json",
success: function(data) {
alert("in success");
},
error: function(jqXHR,error, errorThrown){
//alert("There was an error loggin in");
alert("responseXML ",jqXHR.responseXML);
alert("responseText: ",jqXHR.responseText);
alert("errorThrown: ",errorThrown);
}
});
I have a asp.net web service, which was returning response as...
<?xml version="1.0" encoding="utf-8"?>
<string>{"UserID":"180339206","LogInName":"Amol Chakane","IsValid":"True","UserRoleID":"1","IsPending":"0","IsOrganization":"False"}</string>
Response from jomsocial web service is...
["UserID : 475", "LogInName : kruti patil", "IsValid : True", "UserRoleID : 1", "IsPending : 0", "IsOrganization : False"]
I searched for this issue, but couldn't find solution.
Please help me in this.
Edit #1
Tried to get response in other format.
{
"UserID": "475",
"LogInName": "kruti patil",
"IsValid": "Yes",
"UserRoleID": "1",
"IsPending": "NO",
"IsOrganization": "No",
}
Still it's not working :(
Thanks
When you posted more information I think I found the/a reason for your trouble.
According this page the syntax of your json object is wrong. The linked page shows examples for the most common datatypes in Javascript. If you read the examples, take an eye of the position of quotation signs.
After lot of search finally I found solution to this issue. :)
Instead of localhost need to use 10.0.2.2 this network address
coz android emulator doesn't recognize localhost
refer this
Now at least success callback is called
ajax call is..
$.ajax({
type: "POST",
url: "http://10.0.2.2:5454/kisan-06/index.php?option=com_api&format=raw&app=users&resource=login&key=dfd8a84f8cdce807ae1d30a838415ea37eaa075c",
data: {username: 'sai.kiks2#gmail.com', password: '123456'},
success: function(data, textStatus, jqXHR) {
alert("in success");
},
error: function(jqXHR, textStatus, errorThrown){
alert("There was an error loggin in");
}
});
But now I am facing another problem...
How to access data returned in success callback?