PHP web service not working from jQuery AJAX - php

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?

Related

Unable to get values in POST php web serivce

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
}
});

CROSS DOMAIN ISSUE--jsonp not working

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(){
}
});

Format of jsonp response

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

AJAX headers ending up in HTTP_ACCESS_CONTROL_REQUEST_HEADERS on PHP side

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 ;)

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.

Categories