Why does JQuery $.post send the first variable with a leading ampersand? - javascript

This small snippet of code I'm using is causing issues because the request string is coming through to apache as "delete.php&id={id}&confirm=yes" instead of "delete.php?id={id}&confirm=yes".
if(result) {
var posting = $.post( '/support_ticket/delete.php', {id: tickId, confirm: 'yes'});
posting.done(function( data ) {
var content = $( data ).find( '#main' );
bootbox.alert(content);
});
}
As far as I can recall I've never had this issue before and don't understand why it's doing this.

Related

WP REST API Post Status Using JavaScript

I am getting my hands dirty in WP REST API. I have gone through a couple of tutorials and have a question using JavaScript to create a new post.
In this tutorial, the post var status = 'draft'; (see code). So I am just worried that won't anyone able to hack that status?
jQuery( document ).ready( function ( $ ) {
$( '#post-submission-form' ).on( 'submit', function(e) {
e.preventDefault();
var title = $( '#post-submission-title' ).val();
var excerpt = $( '#post-submission-excerpt' ).val();
var content = $( '#post-submission-content' ).val();
var status = 'draft'; // this code
var data = {
title: title,
excerpt: excerpt,
content: content
};
$.ajax({
method: "POST",
url: POST_SUBMITTER.root + 'wp/v2/posts',
data: data,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', POST_SUBMITTER.nonce );
},
success : function( response ) {
console.log( response );
alert( POST_SUBMITTER.success );
},
fail : function( response ) {
console.log( response );
alert( POST_SUBMITTER.failure );
}
});
});
} );
Users can always manipulate the data being sent in javascript/jquery requests since it all lives client-side
It is not possible to ensure what data is coming from a client. This passes the burden to the server to validate each incoming request. The only way to guarantee a user doesn't submit a different value than expected would be to handle that server-side. Anything you do client side can be manipulated by anyone visiting your site.

How to extract converted value from response using jquery ajax method?

I am using this link :freecurrencyconverterapi to get the converted value from USD to INR.
As you can see in developer mode of browser that the response is {"USD_INR":64.857002}.
Since I am new to programming, is there a way to get the float value using jquery ajax .
Thanks in advance.
That is returning a JSON object.
You need to assign that response object to a variable in your code, so ultimately it will end up looking like below...
var currency = { USD_INR: 64.857002 };
Then you can access it like this:
currency.USD_INR // This will give you 64.857002
See example below..
Edit: As per Rory's code (adapted)...
var currency;
$.ajax({
url: 'https://free.currencyconverterapi.com/api/v4/convert?q=USD_INR&compact=ultra',
dataType: 'jsonp',
success: function(data) {
currency = data.USD_INR;
console.log(currency);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The url that u provided had an issue, so I took an unorthodox route to get what u wanted;
$.get( "https://cors-anywhere.herokuapp.com/https://free.currencyconverterapi.com/api/v4/convert?q=USD_INR&compact=ultra", function( data ) {
$( ".result" ).text( data );
document.getElementById("result").innerHTML = JSON.stringify(data);
console.log(data);
//alert( "Load was performed." );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="result">
</div>

How to share data between programming languages

Hypothetical here: Let's say that you have a JavaScript file that uses jQuery to get input from the user. For example:
var $input = 0;
var buttonClick = function() {
$('.button').click(function() {
$input = $('input').val();
});
}
$(document).ready(buttonClick());
But what if you want to use sql to see if the input matches a password in a database
How would you tell the sql script to run after you click the button?
How would you share the $input variable the sql script so it could check it against the database?
var buttonClick = function() {
$('.button').click(function() {
$.post( "script.php", { Username: "Your Name", Password: "Password" })
.done(function( data ) {
alert( "Data Send: " + data );
// In data you have a response you got from the php script
});
});
}
If you wan't to check Password when your client click on the button , you need to use Ajax and call a Php function.
So when your client click on the button, Jquery call the PHP script and the PHP script call Mysql. After Mysql give a response to Php who send Data to Jquery (true or false in this case).

check file exists on folder on server using jquery ajax

I am trying to check file exist on server using ajax. i have used below script
my server name is like www.Mydomain.netp/Application_Folder/
var fileobj="../invoices/"+filename+".pdf";
var pdfurl;
$.ajax({
url: fileobj, //or your url
success: function(data){
alert('exists');
pdfurl = "http://Mydomain.orgi/Application_Folder/Invoices/" + Invoiceid + ".pdf";
window.open(pdfurl, "PopupWindow", "width=600,height=600,scrollbars=yes,resizable=no");
},
error: function(data){
alert('does not exists');
pdfurl = "http://AnotherDomain.orgi/Invoices/" + Invoiceid + ".pdf";
window.open(pdfurl, "PopupWindow", "width=600,height=600,scrollbars=yes,resizable=no");
},
});
If file exists that time also i am getting into error part,
any other alternative ways to do this. above script perfect with localhost but not working on production environment
The code snippet you provided seems to use calls from two different domains "Mydomain.orgi" and "AnotherDomain.orgi". You need to check if the another server you are requesting follows CORS. I will suggest console/debug your code And another minor thing that a variable named Invoiceid has been used. Please check if this also resolves well.
As long as the file existence is to be checked and as you have already included jquery on your page, I would just try the following
$.get( "ajax/test.html", function( data ) {
$( ".result" ).html( data );
alert( "Load was performed." );
});
or if I follow the promise pattern I would go for the following:
var jqxhr = $.get( "example.php", function() {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
For more here

Reading a json response in jquery

i am trying to reading json response from server by using jquery in webpage, this is my json response {"code":0,"message":success"}, and this is my jquery code . this code does not sent data to server and does not read json response from server. please help me to resolve the problem.
$("#myForm").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
/* get some values from elements on the page: */
var $form = $( this ),
userid=$form.find(input[name="userid"]).val(),
file = $form.find( 'input[name="file"]' ).val(),
filename = $form.find( 'input[name="filename"]' ).val(),
url = $form.attr( 'action' );
/* Send the data using post */
var posting = $.post( url, { userid: userid,file:file,filename:filename } );
/* Put the results in a div */var obj = jQuery.parseJSON( '{ "code": "0" }' );
$.getJSON(url, { get_param: 'code' }, function(data) {
$.each(data, function(index, element) {
$('body').append($('<div>', {
text: element.name
}));
});
});
});
You have syntax error, in the find method quotes is missing :)
userid=$form.find(input[name="userid"]).val(),
cange to
userid=$form.find('input[name="userid"]').val(),
PS: All browser have a javascript console, that show u where is script crashed

Categories

Resources