AJAX - Get cross-origin XML data with username and password - javascript

I'm trying to do a cross-origin request to a server to retrieve some XML data via AJAX. The XML data is password protected and I have to enter credentials in order to access it. I'm trying to autopopulate these fields so that obviously me nor the user has to write them in every time they want to retrieve some data but can't seem to succcessfully pass these through with my request as it still prompts me for a username and password. Here's my code:
$.ajax({
type: "POST",
url: "https://example.com/api",
data:"?&mydata=1&moredata=2",
dataType: "jsonp xml",
crossDomain:true,
beforeSend: function(xhr){
xhr.withCredentials = true;
},
username:"user",
password:"pass",
success: function(xml){
console.log(xml)
}
});
I'm pretty suer i've added all the necessary configuration. Can anyone spot if i've missed something or if there's an alternative way to do this?
thanks

Related

Unsuccessfully fetch data from PHP file

I'm trying to fetch data from a website; however, it return a PHP file as text without the result that I look for (the result after enter all required input) after sending out all the information by using 'POST' method to the server
Down here is my code that I used to fetch info:
var form = {
'cp': poke_cp,
'p_id': poke_id,
'hp': poke_hp,
'dust': poke_dust,
'up': "1"
};
$.ajax({
type: 'POST',
url: "https://pokefind.co/ivcalc.php",
dataType: "text",
data: form,
success: function (data) {
console.log(data);
}
});
As said in comments by #icecub, if the remote server does not allow cross origin requests you will not be able to fetch datas from the website.
The page you are trying to POST seems be the page that handle a form. So you have two problems :
CSRF, during the form handle there is a verification to be sure that you come from the form page
Cross Origin possibly not allowed.
If the website has a WebService you should use it.

Javascript Cross domain URL request

Is it possible to send the cross domain URL request and read the response using JSONP?
Could you please give me some samples?
I am trying to send URL request to a different domain using xhr but couldn't read the response
Please help
Thanks in Advance
You can check with blow example:
$(document).ready(function(){
$.ajax({ // ajax call starts
//crossOrigin: true,
type: "GET",
url: 'http://www.google.com', // JQuery loads areas
dataType: 'json', // Choosing a JSON datatype
async: false,
success: function(data) // Variable data contains the data we get from serverside
{
console.log(data);
}
});
});

Username & password authentication with passing other parameters in Javascript/jQuery

I am trying to send SMS from an SMS provider API with username and password authentication including other parameters using Javascript. More elaborately, I have to send SMS using the following link :
http://sms-pp.sapmobileservices.com/cmn/account_name/account_name.sms
I have to pass other parameters including message text. I have tried creating form and passed other parameters as value of hidden input fields. But it doesn't work. finally, I have used ajax as following :
$.ajax({
url: 'http://sms-pp.sapmobileservices.com/cmn/account_name/account_name.sms',
method: 'get',
dataType: 'json',
async: false,
data: '{Text:A sample message}',
beforeSend: function(req) {
req.setRequestHeader('Authorization', make_base_auth('username', 'password'));
},
success: function (){
alert('Thanks for your comment!');
}
});
It outputs '401 Unauthorized'. can anyone help me with the this.
First of all, the HTTP request type is POST.
Second, the data you are trying to send is not in good format.
Third, the data type should be text.
Having that in mind, you need to send the data in the following structure:
[MSISDN]
List=
[MESSAGE]
Text=
Binary=
Length=
[SETUP]
Class=
DCS=
PID=
MobileNotification=
MailAckType=
ValidityPeriod=
DestinationPort=
OriginatorPort=
[END]
Check this documentation for more info : https://community.sapmobileservices.com/sybase/attachments/sybase/EnterpriseTKB/84/3/SAP_SMS_365-enterprise_service-SMTP_Interface_Specification%20-%20v2.pdf

jQuery AJAX Cross Domain with BASIC Authentication

I'm attempting to make use of the Beanstalk (beanstalkapp.com) API by pulling data into a webpage so people can view it without accessing my SVN.
What I'm doing to try and access it is by using an AJAX request through jQuery. The code is below, but I get an error each time, and can't return the data.
<script type="text/javascript">
$(document).ready(function() {
var tok = 'username' + ':' + 'password123';
hash = btoa(tok);
authInfo = "Basic " + hash;
$.ajax({
url: "http://username.beanstalkapp.com/api/changesets.json",
beforeSend: function (xhr) { xhr.setRequestHeader ("Authorization", authInfo); },
type: "GET",
async: false,
crossDomain: true,
dataType: "json",
success: function(html){
console.log(html);
},
error: function(html){
console.log('error');
}
});
});
</script>
If I access the URL straight through my browser (http://username.beanstalkapp.com/api/changesets.json) it works just fine and returns the json. However, I cannot get the AJAX to return it. Any help is appreciated. Thanks!
You will need to make proxy for cross-domain ajax requests.
Usual scenario looks like this:
Client send ajax request to server
Your server forwards request to external/remote server
Waiting on response from remote server
Parse and process response from remote server
Send response back to client
If you are using php you can send requests with curl, and it is pretty easy to implement. I have wrote article on this topic recently http://www.svlada.com/proxy-ajax-requests-curl-and-symfony-2/.
you cant get a json from other domain than yours. this is a security issue called same origin policy to get over it use JSONP not JSON.
Check this jsfiddle. The username and password is incorrect. Give the correct username and password and check it once again.

GAE blobstore url error: GET not supported

I am having trouble with the google app engine blob store. I am running in the development environment (ie local on my machine.)
Heres what i am doing...
once the form pops up i call into a servlet to generate the URL like this
String url = blobstoreService.createUploadUrl("test/joi");
once i have that i save it in my java scrip and then once the user submits the form i am doing this
$.ajax({ url: self.url,
type: "POST",
//crossDomain: true,
dataType: "jsonp",
//dataType: "multipart/form-data",
success:
function(response, textStatus, jqXHR)
{
alert("saved.");
}
});
}
however, when i do that i get the following exception
GET 405 (HTTP method GET is not supported by this URL) jquery.js:4
i am really struggling with this and any help would be greatly appreciated!
Apart from any other issues, the blobstore expects file uploads in multipart form format; you're attempting to post to it using jquery. If you want to do the post in javascript, you will need to format the body of the POST request appropriately.

Categories