I am trying to upload images using file-uploader jquery plugin. But i am getting "Cross-Origin Request Blocked" and not getting any response. Here is one thing my file is place on another server and my ajax url is different
Please check below my code:-
var url = 'mysite/upload.php';
$('#fileupload').fileupload({
url: url,
dataType: 'json',
done: function (e, data) {
alert(data)
}
});
Please update me how this will fix.
Related
I'm new to this community and a beginner with programming. I'm trying to post a JSON data with AJAX to another PHP file and my code is as follow:
--- AJAX code ---
<?php session_start(); ?>
//other codes in between
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
//alert('Transaction completed by ' + details.payer.name.given_name);
alert(data.orderID);
// Call your server to save the transaction
return fetch('/paypal-transaction-complete', {
method: 'post',
body: JSON.stringify({
orderID: data.orderID
})
});
});
var orderid_data = data.orderID;
$.ajax({
url: "test_parse.php",
type: "POST",
data: {'order_ID': orderid_data },
success: function(data){
alert(data);
});
--- PHP code ---
var_export($_POST);
echo $_POST["order_ID"];
I've realised that my $_POST return an empty array.
Any help would be greatly appreciated :)
Since you open [...]test_parse.php directly in your browser, there is no POST-data present anymore, therefor its empty.
The POST-Data is part of the request. If you open it manually in the browser you perform a GET request.
What can you do to see the var_export?
You can use Chrome or Firefox for example both have Developer-Tools which allow you to trace the exact POST-Request you performed via ajax.
For Chome:
Hit F12 to open the Dev Tools
Navigate to Network
Now perform your Ajax-Post-Request and find it in the Network list. By clicking on the requests you will see all details you will need. Most Imporant for you is Response
I'm trying to create a simple "Register your interest" action. I have the front end working but I'm not sure how to link the AJAX to the PHP file. Where do I put the PHP file?
My current code for the AJAX is:
$.ajax({
url: "register-interest.php",
type: "GET",
dataType: "json",
data: {
type : "registerInterest",
email : userEmailLog,
user : userNameLog,
product : productTitle,
sku : productSku
},
success: function (response) {
JSON.stringify(response);
},
error: function (err) {
JSON.stringify(err);
},
complete : function() {
//$('.user-accept').addClass('unhide');
loading();
}
});
First you check url variable is correct or not and then provide response any data like
success: function (response) {
JSON.stringify(response);
},
Your url value is:
"register-interest.php"
This implies that the php file that processes the AJAX request needs to be available at the same directory level as the page that includes the javascript file that is performing the AJAX request.
Example:
If your page is at http://example.com/my/ajax/page.html
Then the javascript will perform the AJAX request to the URL http://example.com/my/ajax/register-interest.php
Alternatively if you change the JS url value to read "/register-interest.php", then the AJAX request will be made to: http://example.com/register-interest.php
Where you need to put it on your server depends on how your web server's webroot's folder structure is organised, but you should be able to work back from the URL the javascript will be requesting to work this out.
I have the below function that I used in two different pages and worked perfectly in both cases. Suddenly my pages stopped updating and I got the error $.ajax not a function.
function update_report(data) {
var request = $.ajax({
url: "report?poids="+data,
type: "GET",
dataType: "html"
});
request.done(function(msg) {
$("#yw2").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
req ="a";
}
I then changed the $ to jQuery. Then it started sending AJAX requests but it didnt get the relative URL. Like I was generating the request from a page like /index.php/link1 and my code was sending request to /index.php/report rather then /index.php/link1/report
So, then I changed my url: to "link1/report=?"+data then it started sending request to the right page but couldnt update the response as it didnt find the div #id. Got error
TypeError: $(...) is null
What can cause all this issue and how to fix this? And why $.ajax suddenly stopped working, though I didnt make any changes?
JQuery can be not available by $.ajax(). Check if jQuery.ajax() is available or you included jquery twice.
You should not require jquery manually. The best way to include jquery is using Yii::app()->clientScript->registerCoreScript('jquery'). Path for library is set in config/main.php.
Hello im trying to upload a xlsx file one by one so i can show a status bar, the problem is, i did it with a for loop and while loop sending a request via ajax, but when is on the 40th element it stops and the console shows POST (site.php) net::ERR_EMPTY_RESPONSE, i tried to do it in the localhost and it works perfect, but when a i try to do it on my external server(godaddy) it shows the error. here is the code.
for(j=1;j<=tama;j++){
$.ajax({
type: "POST",
url: "ejphp.php",
dataType: "json",
data: {vals: regs, j:j},
success: function(datos){
console.log(j)
prog=datos['progreso_r'];
var id_vac=datos['id_vac']
var tipo=datos['tipo']
var tipo2=datos['tipo2']
var tot_ing=datos['tot_ing']
prog_p=Math.round(prog*100/tama);
$("#progressbar").val(prog_p);
$("#progreso").text(prog_p+'%');
$("#datos_vac").text('Id Vacuno: '+id_vac);
if(prog_p==100){
$("#aceptar").show("slow");
}
if(tipo=='error') registro(tipo2, id_vac)
subir(parseInt(prog)+1);
return false;
}
})
}
Maybe your server can't pass more than 40 requests, try to increase php memory limit and execution time. And try to browse in godaddy's admin panel.
I'm trying to implement a simple api request to the SEOmoz linkscape api. It works if I only use the php file but I want to do an ajax request using jquery to make it faster and more user friendly. Here is the javascript code I'm trying to use:
$(document).ready(function(){
$('#submit').click(function() {
var url=$('#url').val();
$.ajax({
type: "POST",
url: "api_sample.php",
data: url,
cache: false,
success: function(html){
$("#results").append(html);
}
});
});
});
And here is the part of the php file where it takes the value:
$objectURL = $_POST['url'];
I've been working on it all day and can't seem to find the answer... I know the php code works as it returns a valid json object and displays correctly when I do it that way. I just can't get the ajax to show anything at all!!
...data: 'url='+encodeURIComponent(url),