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
Related
update: I would like to pass the var value to the server
hello,
same old, same old ... :)
I have a form called <form id="testForm" action="javascript:test()"> and a code area called <code id="testArea"></code>
I am using this code to stringify and display the data in the code area:
var formData = form2object('testForm');
document.getElementById('testArea').innerHTML = JSON.stringify(formData, null, '\t');
var value = JSON.stringify(formData, null, '\t');
What I want is to send this data to a JSON file.
I've been working on this project : http://ridegrab.com/profile_old/ and if you press Submit Query button you will see the head of the page populate.
Also I want use this piece of script to send data:
function authenticate(userName, password) {
$.ajax
({
type: "POST",
//the url where you want to sent the userName and password to
url: 'username:password#link to the server/update',
dataType: 'json',
async: false,
//json object to sent to the authentication url
data: '{"userName": "' + userName + '", "password" : "' + password + '"}',
success: function () {
alert("Thanks!");
}
})
}
Again, all I want is to be able to send that JSON data to the server. My server is set up to update or POST the data in the right place.
You post JSON like this
$.ajax(url, {
data : JSON.stringify(myJSObject),
contentType : 'application/json',
type : 'POST',
...
if you pass an object as settings.data jQuery will convert it to query parameters and by default send with the data type application/x-www-form-urlencoded; charset=UTF-8, probably not what you want
'data' should be a stringified JavaScript object:
data: JSON.stringify({ "userName": userName, "password" : password })
To send your formData, pass it to stringify:
data: JSON.stringify(formData)
Some servers also require the application/json content type header:
contentType: 'application/json'
There's also a more detailed answer to a similar question here: Jquery Ajax Posting JSON to webservice
In case you are sending this post request to a cross domain, you should check out this link.
https://stackoverflow.com/a/1320708/969984
Your server is not accepting the cross site post request. So the server configuration needs to be changed to allow cross site requests.
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
Whenever i am sending Korean text in AJAX request with parameter server receive it as junk value.
I need solution for this.
Thanks.
Code:
$.ajax({
url : '/login?username='+username,
cache: false,
type : 'post',
dataType : 'text',
contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
success:function(data){
alert('success');
}
});
Hi Deceze, in the server side we have "Spring Controller",
and the method looks like
#RequestMapping(value="/login", method=RequestMethod.POST, consumes="application/x-www-
form-urlencoded;charset=UTF-8")
public #ResponseBody String login(#RequestParam("username") String username) {
// Code
}
And the received username is "íêµ" when i enter "한국" (korea).
You are sending username as query parameter.
Which happens when you do get.
Use "contents" to send data.
Please refer https://api.jquery.com/jQuery.ajax/
I have a function which currently passes an account code (derived from a combo box) to the server. Currently it does this by sending the request in the body - I need it to send as a URL parameter. So for example the URL should be:
localhost:1234/myProject/WebApp/Data?accountCode=Full
Assuming full is selected.
My code below works as a request body but my attempts to amend it to submit as a URL request have failed.
accountSelected: function () {
var saccountCode = $("select#accountcombo").val();
var stringAccountCode = saccountCode.toString()
console.log("Account is: " + stringAccountCode);
var myURL = "WebApp/Data";
$.ajax({
url: myURL,
type: "POST",
data: {
"accountCode": stringAccountCode
},
dataType: "text",
})
I have been looking at using $.param but couldn't get anything to work and also read on other questions about using $.get but when I change my code above to a "GET" i get an error
"Request method 'GET' not supported" - the server is expecting a POST request. Any way i could achieve this?
Thanks
Try,
URL: "localhost:1234/myProject/WebApp/Data?accountCode="+stringAccountCode
Appending number of parameters you want example
?accountCode="+stringAccountCode+"&aa="+someAccount
Okay, I'm having some suicidal issues posting a JSON string to a PHP page. I have literally been through the top ten results on Google and plenty of SO questions related to my problem, but still can't work out what I'm doing wrong.
I have multiple forms on a page and want to collect all form fields, turn them into a JSON string and post them to a PHP page, where a script iterates each item and updates the relevant database tables.
This is my jQuery/JS script to collect the data from all the forms:
var photo_annotations = {};
$('form').each(function(i) {
var id = $(this).attr('id');
photo_annotations[id] = {
caption: $('#'+id+'_caption').val(),
keywords: $('#'+id+'_keywords').val(),
credit: $('#'+id+'_credit').val(),
credit_url: $('#'+id+'_credit_url').val()
};
});
If I console.log my photo_annotations object, this is what is produced, based on a two form example:
({11:{caption:"Caption for first photo.", keywords:"Keyword1,
Keyword2, Keyword3", credit:"Joe Bloggs",
credit_url:"www.a-domain.com"}, 12:{caption:"Caption for Lady Gaga.",
keywords:"Keyword3, Keyword4", credit:"John Doe",
credit_url:"www.another-domain.com"}})
I then need to POST this as a string/JSON to a PHP page, so I've done this:
$.ajax({
type: 'POST',
dataType: 'html',
url: 'ajax/save-annotations.php',
data: { data: JSON.stringify(photo_annotations) },
contentType: "application/json; charset=utf-8",
success: function(data) {
if (data) {
$('#form_results').html(data);
} else {
alert("No data");
}
}
});
And on my PHP page, I've got this:
<?php
//print_r($_POST['data']);
$decoded = json_decode($_POST['data'],true);
print_r($decoded);
?>
Now, this isn't the only thing I've tried. I've tried to remove all the JSON settings from the AJAX script, in a bid to just send a pure string. I've tried removing contentType and JSON.stringify but still won't go. My PHP page just can't get the data that I'm sending.
Please help push me in the right direction. I've got to the point where I can't remember all the variations I've tried and this little script is now on day 2!
MANAGED TO FIX IT
I rewrote my AJAX function and it worked. I have no idea what was going wrong but decided to test my AJAX function with a very basic data string test=hello world and found that no POST data could be read from the PHP page, even though Firebug says that the page did in fact receive post data matching what I sent. Very strange. Anyway, this is the revised AJAX script:
var the_obj = JSON.stringify(photo_annotations);
var post_data = "annotations="+the_obj;
$.ajax({
url: 'ajax/save-annotations',
type: 'POST',
data: post_data,
dataType: 'html',
success: function(data) {
$('#form_results').html(data);
}
});
Try:
$.ajax({
// ...
data: { data: JSON.stringify(photo_annotations) },
// ...
});
If you just set the "data" property to a string, then jQuery thinks you want to use it as the actual query string, and that clearly won't work when it's a blob of JSON. When you pass jQuery an object, as above, then it'll do the appropriate URL-encoding of the property names and values (your JSON blob) and create the query string for you. You should get a single "data" parameter at the server, and it's value will be the JSON string.
Try urldecode or rawurldecode as follows:
<?php
$decoded = json_decode(urldecode($_POST['data']), true);
print_r($decoded);
?>
I rewrote my AJAX function and it now works. I have no idea what was going wrong but decided to test my AJAX function with a very basic data string test=hello world and found that no POST data could be read from the PHP page, even though Firebug says that the page did in fact receive post data matching what I sent. Very strange. Anyway, this is the revised AJAX script:
var the_obj = JSON.stringify(photo_annotations);
var post_data = "annotations="+the_obj;
$.ajax({
url: 'ajax/save-annotations',
type: 'POST',
data: post_data,
dataType: 'html',
success: function(data) {
$('#form_results').html(data);
}
});
The only thing I can think of is that the order of AJAX settings needed to be in a particular order. This is my old AJAX script which does not send POST data successfully - well it does send, but cannot be read!!
var the_obj = JSON.stringify(photo_annotations);
var data_str = "annotations="+the_obj;
$.ajax({
type: 'POST',
dataType: 'html',
data: data_str,
url: 'ajax/save-annotations.php',
success: function(data) {
$('#form_results').html(data);
}
});
in your ajax call try resetting the dataType to json
dataType: "json",
You wouldn't have to use the JSON.stringify() either. On your php script you won't have to decode [json_decode()] the data from the $_POST variable. The data will be easy readable by your php script.