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/
Related
I have a problem sending ajax data from my javascript file to my c# controller. I get a "bad request error" in my c# program, and the reason i get that is because the data parameter "result" which i am sending with ajax is not getting received by c# and the c# variable stays null. I know Ajax is routing to the correct controller since it is calling the method, but the variable "result" is not getting received by c# for some reason.
Here is my ajax request.
$.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
data: { 'result' : result },
url: "https://localhost:44374/api/task",
cache: false,
success: function (data) {
// Process the received data.
}
});
Here is my c# controller
[HttpPost]
public ActionResult<string> Get(string result)
{
string id = result;
getTaskContent(id);
return id;
}
After changing Ajax to GET, the program works and the output is:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:44374/api/task/1108164994166723?_=1549876832637 application/x-www-form-urlencoded; charset=UTF-8
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 17.8526ms 404
But for some reason the C# Actionresult method is not getting executed.
See that the URL is localhost:44374/api/task/1108164994166723?_=1549876832637, where the result variable is 1108164994166723, what I have no idea about is how the ?_=1549876832637 part is coming. If I alert the result variable in the window it is only 1108164994166723
Solution
The combination of changing to GET instead of POST and the changing URL in Ajax to url: "localhost:44374/api/task?result=" + result, did the job.
Correct Ajax code is:
$.ajax({
type: 'GET',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
url: "https://localhost:44374/api/task?result=" + result
});
try changing the content-type to "application/json;charset=utf-8" and sending the parameter name in the URL like this:
$.ajax({
type: 'POST',
contentType: "application/json;charset=utf-8",
url: "https://localhost:44374/api/task?result=" + result,
cache: false,
success: function (data) {
// Process the received data.
}
});
Change the GET method in the controller to POST method. Since in your ajax call you are specifying the type as post.
you must changeto HttpPost in Controller and change the result type to JsonResult.
Try to change your type to type:GET -- because your controller action method is a HTTPGET and contentType to: contentType: 'application/json; charset=utf-8
and data to data: JSON.stringify({ result: result })
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 have an ajax POST method that, despite what I am sending to the server, is apparently appending a '↵' character to the value field. My code:
$.ajax({
url: url,
type: "POST",
data: {"name" : "lol"},
dataType : "json",
contentType : "application/json; charset=utf-8"
});
The error returned is error code 400 "Client submitted invalid JSON: lexical error: invalid string in json text.↵" and the console reports that this name=lol↵ is the data being sent.
I just read the error message again and checked with the docs here.
What you need to do is to send your request in JSON format, not as form-data. You specified the right contentType, but you need to convert your data using JSON.stringify
Look at this answer here for a possible solution.
In your case it would be something like this:
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify({
name: 'lol',
}),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
});
I hope that works for you. If this didn't work either, I would check with the docs at dev.groupme.com. Maybe you have misspelled some of the JSON fields?
i'm using a vert.x server with a small javascript code as follow:
$.ajax({
type: 'POST',
url: 'http://localhost:8080',
data: {id:content},
dataType: 'json',
}).done(function(data) {alert("succes")+data;})
.fail(function() { alert("fail");})
.always(function() { alert("complete");});
When i run my program, i have the alert("succes") popup (which mean i did my post and have answer in return right ?) but the variable data is empty. In my java code i'm doing this to send an answer:
request.response().putHeader("content-type", "json")
.end(Json.encodePrettily(content));
Where content is just a string with what i post on the server with the POST request.
If i have response of my server, why the data field of my js code is still empty ?
Am i doing this wrong ?
Change your alert to this:
alert("success \n" + JSON.stringify(data));
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