I am trying to do the following
$("select.languages").on("change", function(){
var lang = $(this).find("option:selected").text();
$.get(url: "/search",
data: {"lang": lang},
dataType: 'script');
});
so every time a language is selected I would call /search with the language and by other SO answers to make dataType be "script"
but this doesn't quite work. What does work is
$.get( "/search.js",{"lang": lang} )
yet this pollutes the url since the format is very explicit and doing using this method requires me to add extra pointless code
can I implicitly set the response format?
Try this instead:
$.ajax({
url: "/search",
data: {"lang": lang},
dataType: 'script'
});
The get shorthand method does not accept the parameters as an object like this.
Probably a syntax error - I would try this:
$("select.languages").on("change", function(){
var lang = $(this).find("option:selected").text();
$.ajax({
url: "/search",
data: {"lang": lang}
});
});
Since you're trying to send a JS request, you probably won't even need the dataType property, as Ajax expects it by default:
$.ajax - dataType
Related
I am creating a DDBB Insert trough $.ajax:
$(document).on('click','.submitMessage', function(){
content=$('textarea').val();
img=$('#messageImg').val();
stdMsg=$('.ms_stdMsg').val();
prefix=$('.prefix').val();
phone=$('.cuadroTelefono').val();
$.ajax({
url: "../actions/newMessage.php",
type: "POST",
data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
contentType: false,
cache: false,
processData:false,
success: function(data)
{
alert("Enviado");
}
});
});
And this is the way I receive code on newMessage.php:
$ms_content = $_POST['ms_content'];
$ms_img = $_POST['ms_img'];
$ms_prefix = $_POST['ms_prefix'];
$ms_phone = $_POST['ms_phone'];
Console gives an error
Notice: Undefined index: ms_content in C:...\newMessage.php on line 9
one for each variable passed (I have ommited entire URL)
As the posted information is an object, I guess I must decode it someway on PHP, but trying:
$ms_content = json_decode($_POST['ms_content']);
...has neither workd
You need to specify the data that you are sending with contentType parameter. For more references
$(document).on('click','.submitMessage', function(){
content=$('textarea').val();
img=$('#messageImg').val();
stdMsg=$('.ms_stdMsg').val();
prefix=$('.prefix').val();
phone=$('.cuadroTelefono').val();
$.ajax({
url: "../actions/newMessage.php",
type: "POST",
data:{ms_content:content, ms_img:img,ms_prefix:prefix,ms_phone:phone},
cache: false,
success: function(data)
{
alert("Enviado");
}
});
});
Please remove processData:false, contentType: false and then try.
You can use this $_REQUEST instead of $_POST in your php file.
$ms_content = $_REQUEST['ms_content'];
Instead of
$ms_content = $_POST['ms_content'];
Second Maybe due to url path
try giving full url path.
Third
Provide a contentType.
I think you have to access the $_GET parameters because jquery documentation says:
data
Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).
http://api.jquery.com/jquery.ajax/
So you will get your data with
$ms_img = $_GET['ms_img'];
In my function I am calling method passing $obj as a parameter in the function. I have defined currsel object, now in the method its showing me $obj.currsel.html() is not a function. I am sharing my code below.
$(document).on('change','.ui-modulelist1',function(e){
var $targetSel =coreFrameWork.frModuleContId;
var $thisVal= $(this).find("option:selected").val();
var loadUrl = '../ux_framework_New/_'+$thisVal+'/index.html';
commonFramework.initLoad({'url':loadUrl, 'currSel':$targetSel, 'mode':'autoload'});
});
var commonFramework ={
initLoad:function($obj){
$.ajax({
type: "GET",
url: $obj.url,
dataType: "html",
cache:false,
async:false,
beforeSend: function(xhr){
xhr.withCredentials = false;
},
success: function(html) {
$obj.currSel.html(html);
$obj.currSel.attr(projCommonAttr.autoloadAttr,'loaded');
commonFramework.contentSpecific({'currSel':$obj.currSel,'mode':$obj.mode});
},
error: function(qXHR, textStatus, errorThrown){
}
});
},
It's hard to tell without seeing an example url. What is coreFrameWork.frModuleContId? Try logging it in your console. I'm guessing you need to do
var $targetSel = $(coreFrameWork.frModuleContId);
Is this:
var $targetSel =coreFrameWork.frModuleContId;
a jQuery object?
The function html() belongs to jQuery, so if your object is not a jQuery one, it won't find that method.
I don't exactly know what that variable is, but you can try by wrapping it into a jQuery object:
var $targetSel = jQuery(coreFrameWork.frModuleContId);
Also, it is not a good practice to use names beginning with $ to name simple js objects (those names are often used to name variables representing jQuery objects or so).
Hope this helps you.
Regards,
Marcelo
url: $obj.url,
This parameter needs to be a string. If you want a dynamic url from your obj variable do this:
url: $obj+".php", // .php or whatever the file type is of the page you're requesting
Stuck on the first stage of a big project. I am trying to display the JSON value that I get from URL shown below. It shows the data when I past the URL on the a browser. But when I put the URL in variable and try to show in html it doesn't show the phrased value.(I will delete the app/key one I get the solution.) Thanks in advance.
http://jsfiddle.net/rexonms/6Adbu/
http://api.flickr.com/services/rest/?format=json&method=flickr.photosets.getPhotos&photoset_id=72157629130565949&per_page=10&page=1&api_key=ccc93a20a1bb9060fa09041fa8e19fb5&jsoncallback=?
Have you tried using the $.getJSON() or the $.ajax() method? This seems to return the data just fine:
$.getJSON(apiCall, function(data) {
console.log(data);
});
Here's a working fiddle.
Additional Information
It seems like you may benefit from a simple tutorial that explains AJAX in relation to jQuery's $.getJSON() and $.ajax() methods.
$("<span>").html(apiCall)
apiCall is a string (containing the URL). All you're doing here is setting a span to have the URL as its content. You need to actually use AJAX to load said URL.
$.ajax({
url: 'http://api.flickr.com/services/rest/',
dataType: 'jsonp',
type: 'GET',
data: {
format: 'json',
method: 'flickr.photosets.getPhotos',
photoset_id: '72157629130565949',
per_page: 10,
page: 1,
api_key: 'ccc93a20a1bb9060fa09041fa8e19fb5'
},
jsonp: 'jsoncallback',
success: function(data){
console.log(data);
}
});
Inside success, data is the JSON object returned by the API.
i'm trying to send an js object to a php function using jquery.ajax.
This is what i have so far:
js side:
$.ajax({
type: "GET",
dataType: "json",
url: url,
data: {persoon : persoon2},
async: false,
success: function(){
alert(data);
return true;
}
});
php side:
$decode = json_decode($_GET["persoon"]);
$verzekering->setVoornaam($decode->persoon_voornaam);
in js this works: persoon2.persoon_voornaam
but i can't get to the value in php, what am i doing wrong?
few fixes
data: "persoon=persoon2", // check input format
success: function(data) { // missing data argument
EDIT your ajax code is working (check URL or php code)
http://jsfiddle.net/ish1301/KZndE/
Found the problem(s)
I was using this inside Drupal and the Jquery version was still 1.2.6. Upgrading it resolved a lot of the problems
The string i tried to catch with the $_GET["persoon"] was mall formated becaus i just send along a js object. Changing
data: {persoon : persoon2},
to
data: {persoon:JSON.stringify(persoon2)},
fixed the problem
I'm writing a dynamic js that should send a request to an action, that is defined in the href attribute in a link.
The link look like that:
show something
As you can see the link has a get param named callback. That is defined, because the request method should be as generic as possible and the element should know by itself, what it has to do with the response.
Now i send the request with jquery ajax function to the action, looking like that:
jQuery('#mylink').live("click", function() {
jQuery.ajax({
type: 'GET',
dataType:"jsonp",
error: ErrorHandler.catchXhrError,
url: jQuery('#mylink').attr('href');
});
});
That works very well, in principle. But my problem is, that jquery defines its own callback get param and i don't know, how i can tell the method, that the callback param in my links href is the right one and the request-uri is looking like that:
http:/example.com/module/action?callback=MyCallback&callback=jsonp1279196981233
The only way out i see, is to define my own callback-param named mycallback and interpret mycallback in the action instead of callback:
show something
But i don't like this solution, because my request uri has a param that will not be used:
http:/example.com/module/action?mycallback=MyCallback&callback=jsonp1279196981233
Please attend, that i could not define the name of the callback method inside the ajax method
jQuery('#mylink').live("click", function() {
jQuery.ajax({
type: 'GET',
dataType:"jsonp",
jsonpCallback: 'MyCallback',
error: ErrorHandler.catchXhrError,
url: jQuery('#mylink').attr('href');
});
});
because the send Method should be generic and will be used across our whole side and the response has to be processed in many different ways by different js methods.
Hope i could describe my problem sufficiently and would be happy about any help.
You should use the jQuery.ajax version but not hardcode the callback name. Instead parse the href attribute and extract the name of the callback to use..
and here is the parsing..
jQuery('#mylink').live("click", function() {
var href = jQuery('#mylink').attr('href');
jQuery.ajax({
type: 'GET',
dataType:"jsonp",
jsonpCallback: href.match(/([?&]callback[a-zA-Z0-9=]+)/)[0].split('=')[1],
error: ErrorHandler.catchXhrError,
url: href;
});
});
Have you looked into http://code.google.com/p/jquery-jsonp/ ?