This question already has answers here:
How can I access object properties containing special characters?
(2 answers)
Closed 1 year ago.
Im trying to build a ajax call using some $post data that require the score separator and always face an issue of wrong syntax.
$.ajax({
url: "register.php",
type: "post",
cache: false,
dataType: 'json',
data: {
register-username: name,
register-email: email,
},
success: function(response) {
},
error: function(){
}
});
i wonder how i can use those register-username and register-email without having a syntax error. Changing those for no separator is not an option.
Use quotes
data: {
"register-username": name,
"register-email": email,
},
Related
This question already has an answer here:
How to load grid data with json data on ajax sucess
(1 answer)
Closed 4 years ago.
I have a grid where I bind my store in this manner.
bind: {
store:'{myStoreViewModel}'
},
Now in myStoreViewModel I defined like this.
myStoreViewModel:{
autoLoad: false,
proxy: {
type: 'ajax',
url: 'myUrl',
timeout: 240000,
paramsAsJson: true,
actionMethods: {
read: 'POST'
},
reader: {
type: 'json'
}
},
listeners: {
load: 'myLoad',
}
},
Here I am getting my serviceStatus failure. Then i further investigate this with normal Ajax.
Here is my normal Ajax
myNormalAjax : function(myJSON){
Ext.Ajax.request({
url: 'retrievemsgsummary.do',
method: 'POST',
dataType: 'json',
timeout: ProcessConstants.VALBIZDT_TIMEOUT,
jsonData: myJSON,
success: function(response) {
var responseText = Ext.JSON.decode(response.responseText);
if (responseText.serviceStatus == 'SUCCESS') {
this.messageSummaryDetails(this,responseText);
}
},
failure: function(response) {
},
scope: this
});
},
Here I am passing jsonData and serviceStatus is coming sucess.
then In my viewModel also I am trying pass jsonData but no luck. This how i am passing jsonData there.
dataObj : function(myJSONDATA){
var myStoreStore = this.getViewModel().getStore('myStoreViewModel');
myStoreStore.proxy.jsonData = myJSONDATA;
}
Can anybody suggest how to bind my store for my grid.
**
You can pass additional params (extra params) to store's proxy either using the proxy's extraParams config at store/proxy definition, or later in the life cycle using setExtraparam or setExtraparams methods. So, in your case, it could be something like this:
dataObj : function(myJSONDATA){
var myStoreStore = this.getViewModel().getStore('myStoreViewModel');
myStoreStore.getProxy().setExtraParams(myJSONDATA);
}
This question already has answers here:
How to pass parameters to jQuery document.ready() function (ASP.NET MVC, C#)
(4 answers)
How to add parameters to a #Url.Action() call from JavaScript on click
(2 answers)
Closed 4 years ago.
Why does the "action" input parameter not recognized by the ajax call?
My code looks like this:
<script type="text/javascript">
$(document).ready(function (string action) {
$.ajax({
type: "GET",
url: #Url.Action(action, "Default") ,
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
}).done(function () {
alert("Success");
}).error(function () {
alert("Faile");
});
}
</script>
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 4 years ago.
I'm new to JS. I try the function closure, just as in the documentation example and I get an uncaught reference. Why?
function fetchData(filter) {
return $.ajax({
type: "GET",
contentType : "application/json; charset=utf-8",
dataType: "json",
url: "my_url"+route,
data: filter
});
};
function fetchDataSource(filter) {
var route = "data_source";
return fetchData(filter);
};
And now, when I call the function:
var filter;
fetchData(filter);
I have the following error:
Uncaught ReferenceError: route is not defined
at fetchData (:6:49)
at fetchDataSource (:3:10)
at :1:1
Why is route not visible in my function?
Thanks
the fetchData function does not include route in its closure because route is defined inside a sibling function. There are ways to make it closure around route as you would expect, somethign like this would work:
var route;
function fetchData(filter) {
return $.ajax({
type: "GET",
contentType : "application/json; charset=utf-8",
dataType: "json",
url: "my_url"+route,
data: filter
});
};
function fetchDataSource(filter) {
route = "data_source";
return fetchData(filter);
};
Because route is defined in the scope that contains fetchData here, whereas it's not in yours.
This question already has answers here:
Ways to circumvent the same-origin policy
(8 answers)
Closed 8 years ago.
I'm trying to get a time data from json with the code bellow, but i get a error instead of the data, what can be happening?
$.ajax({
type: "POST",
url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
success: function(data) {
alert()
data.location;
$("div").html(data);
},
error: function(data) {
$("div").html('can not get the json');
}
});
Here is a fiddle: http://jsfiddle.net/jodgjqwf/
Is http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro expecting you to POST to it? I just hit the url and it returned the JSON to me.
Change "POST" to "GET".
$.ajax({
type: "GET",
url: "http://www.previsaodotempo.org/api.php?city=Rio+De+Janeiro",
success: function(data) {
$("div").html(data);
},
error: function(data) {
$("div").html('can not get the json');
}
});
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 8 years ago.
I have a PHP script that takes a query string returns and JSON object. I am trying to make an AJAX call that will return this JSON object so I can use it in jQuery autocomplete. Here is my AJAX call so far
$(document).ready(function(){
$("#searchBox").keyup(function(){
var search_result = $.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
return data;
}
});
console.log(search_result);
});
});
This sends the result of the AJAX call to the console ( a javascript object). I can see a JSON in that object. I would think all I have to do to access the data I want is this
console.log(search_result.responseJSON);
But this just gives me undefined. What am I doing wrong here?
That is not the way to do it because it is an async call so the data will not be returned by the ajax call. Try this
$(document).ready(function(){
var search_result;
$("#searchBox").keyup(function(){
$.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
search_result = data;
console.log(search_result);
}
});
});
});
You need to wait for the AJAX call to complete in order to access the data it returns.
$(document).ready(function(){
$("#searchBox").keyup(function(){
$.ajax({
url:"/mm5/service/product_lookup_json.php",
type: "GET",
dataType: "json",
jsonp: false,
data: "query=" + $("#searchBox").val(),
success: function(data){
//data is available at this points
console.log(data);
}
});
});
});