How to pass a single string in AJAX [without key] - javascript

I am trying to pass a single quoted string in data without key.
I write a code below :
$.ajax({
type: "POST",
url: 'MY_API_URL',
data: "HATETH",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (r) {
console.log(r);
}
});
it not works..
Please help me to do this. Is it possible or not?

Related

How to set the multiple same name query in javascript ajax

I have python api which can get the multiple data in same query such as.
createdata?ob=120&st=cool&st=warm
It can get st as ["cool","warm"] in python.
st = request.query_params.getlist("st")
print(st) #["cool","warm"] get the data as array.
Now I want to do the same thing from ajax
I try this
$.ajax({
type: "POST",
url: "defapp/createdata",
method: "GET",
data: {"ob":130,"st":"warm","st":"cool"},
contentType: "application/json",
and this.
$.ajax({
type: "POST",
url: "defapp/createdata",
method: "GET",
data: {"ob":130,"st":["warm","cool"]},
contentType: "application/json",
However former one get ["cool"] as st
later one get [] as st.
It's don't work,because you api not recive a data body,but it recive a params value.
You can changed to these below.
$.ajax({
type: "POST",
url: "defapp/createdata?ob=120&st=cool&st=warm",
method: "GET",
data: {},
contentType: "application/json",
I hope is can work in your case.

Ajax response isn't responding to a json response

The task is very simple I only need to save these json responses into my code to manipulate it later but I can't get to make it function. I've tried more than a strategy with nothing working. Thing is it's working for a json and not the other with the exact same syntax. Here is my code
function searchOwner() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: "jsonp",
url: "https://elvet.eu-gb.mybluemix.net/get_pet_owner?pet_id=p12345678912345",
success: function(responseData) {
alert(responseData)
}
})
}
function searchPet() {
$.ajax({
type: "GET",
contentType: "application/json",
dataType: 'jsonp',
url: "https://elvet.eu-gb.mybluemix.net/getpets?pet_id=p12345678912345",
success: function(responseData) {
alert('ok')
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
The SearchOwner is not working at all while searchPet is working just fine. Any help would be highly appreciated thank you
dataType : "json", in searchOwner() where as it's dataType: 'jsonp' in searchPet()

Ajax JSON access array from the array JSON encode server respond

I have an array JSON encode respond when the ajax JSON throw a post request (refer below).
requestparser.php:
$array = array("phweb" => "yes", "phemail" => "yeeess");
echo json_encode($array);
And this Ajax JSON use for sending post request to requestparser.php and processing the return response.
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]);
alert(result[1]);
}
});
I want to get the value of array key phweb and the value of array key phemail yet when an alert box popup, it says undefined. What seems the problem? Any help, ideas, clues would be greatly appreciated.
So far what I tried is:
$.ajax({
type: 'POST',
url: 'requestparser.php',
data: { "request" : "pull" },
contentType: "application/json; charset=utf-8",
dataType: 'json',
cache: false,
success: function(result) {
alert(result[0]->phweb);
alert(result[1]->phemail);
}
});
And sadly, it doesn't work.
The result is a JSON object. You can access it like this
success: function(result) {
alert(result['phweb']);
alert(result['phemail']);
}

Ajax call Into MVC Controller- URL Issue

I've looked at the previously-posted jQuery/MVC questions and haven't found a workable answer.
I have the following JavaScript code:
$.ajax({
type: "POST",
url: '#Url.Action("Search","Controller")',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
When calling the Url the post looks like:
NetworkError: 500 Internal Server Error -
Why does it return it like this (the logic behind it) and what's a solution?
P.S.: Additional Information: %22 is the URL Encoding Reference for <<">> character
In order for this to work that JavaScript must be placed within a Razor view so that the line
#Url.Action("Action","Controller")
is parsed by Razor and the real value replaced.
If you don't want to move your JavaScript into your View you could look at creating a settings object in the view and then referencing that from your JavaScript file.
e.g.
var MyAppUrlSettings = {
MyUsefulUrl : '#Url.Action("Action","Controller")'
}
and in your .js file:
$.ajax({
type: "POST",
url: MyAppUrlSettings.MyUsefulUrl,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
or alternatively look at levering the framework's built in Ajax methods within the HtmlHelpers which allow you to achieve the same without "polluting" your Views with JS code.
you have an type error in example of code. You forget curlybracket after success
$.ajax({
type: "POST",
url: '#Url.Action("Search","Controller")',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
})
;
A good way to do it without getting the view involved may be:
$.ajax({
type: "POST",
url: '/Controller/Search',
data: { queryString: searchVal },
success: function (data) {
alert("here" + data.d.toString());
}
});
This will try to POST to the URL:
"http://domain/Controller/Search (which is the correct URL for the action you want to use)"
Starting from Rob's answer, I am currently using the following syntax.Since the question has received a lot of attention,I decided to share it with you :
var requrl = '#Url.Action("Action", "Controller", null, Request.Url.Scheme, null)';
$.ajax({
type: "POST",
url: requrl,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
starting from mihai-labo's answer, why not skip declaring the requrl variable altogether and put the url generating code directly in front of "url:", like:
$.ajax({
type: "POST",
url: '#Url.Action("Action", "Controller", null, Request.Url.Scheme, null)',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
Simple way to access the Url Try this Code
$.ajax({
type: "POST",
url: '/Controller/Search',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
});

jquery Post , data object

I try to understand one thing.
I want to post an object with jquery Ajax POST , something like this:
var dataPostYear = {
viewType:GetViewType(),
viewDate:'2009/09/08',
languageId:GetLanguageId()
};
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
and it doesn't work.
But this one works fine:
var dataPostYear = "{viewType:'"+ GetViewType() + "',viewDate:'2009/09/08',languageId:'"+GetLanguageId()+"}";
$.ajax({
type: "POST",
url: url,
data: dataPostYear,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnLoadYearListSuccess,
error: OnLoadYearListError
});
GetViewType() return --'0'
languageId() return --'1'
it's just a string
there is a way to post an object, something what I try to do in my first way ? Or not ?
Thanks
Use jQuery.param(). Here is the documentation
You should look at .postJSON.
Essentially, you just add json as a 4th argument to the $.post
From the site:
// Send the request
$.post('script.php', data, function(response) {
// Do something with the request
}, 'json');
If you want the .ajax call version, you can convert it using the .post docs.

Categories

Resources