CROSS DOMAIN ISSUE--jsonp not working - php

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

Related

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.

using ajax to retrieve an xml response and display results

I know how to post using $.ajax method.
I have a form that posts data to a payment API this is what the response looks like:
<ns2:GetTransactionsResponseType xmlns:ns2="http://www.paymentgateway.com/schema/api/2.0/service.xsd">
<Timestamp>2012-08-14T17:33:55.213+10:00</Timestamp>
<Version>2.0</Version>
<Status>Success</Status>
<total>0</total>
</ns2:GetTransactionsResponseType>
as you can see the total of transactions is 0,
so step 1 make the post request using $.ajax, then on success: I want to do $('#results'),html('the total transactions are: '+numberoftransactions);
any ideas/suggestions ? thank you
you can try something like this
$(function(){
$.ajax({
type: "GET",
url: $uri,
dataType: "xml",
async: false,
contentType: "text/xml; charset=\"utf-8\"",
complete: function(xmlResponse) {
// So you can see what was wrong...
console.log(xmlResponse);
console.log(xmlResponse.responseText);
$("#preForXMLResponse").text(xmlResponse.responseText);
}
});
});
try finding required value in responseText
for more information please visit this link
The XMLHttpRequest Object
I guess rahul has answered your question so I just wanted to add that if you're trying to pass data to the url and get back response accordingly then you could take advantage of the data property while making ajax request.
suppose you're trying to get xml response according to user profile so you have to pass user id to the url to get accurate response. You can just do it like this
$(function(){
$.ajax({
type: "GET",
url: $uri,
data: $user_id, //this id will be passed with the request to the url as query string
dataType: "xml",
async: false,
contentType: "text/xml; charset=\"utf-8\"",
complete: function(xmlResponse) {
// So you can see what was wrong...
console.log(xmlResponse);
console.log(xmlResponse.responseText);
$("#preForXMLResponse").text(xmlResponse.responseText);
}
});
});
Hope this helps or may be I didn't understand properly, what you were looking for.

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