4

I have to send a name and a link from client side to the server. I thought of using AJAX called by Javascript to do this.

This is what I mean. I wished to make an ajax request to a file called abc.php with parameters :-

1. http://thumbs2.ebaystatic.com/m/m7dFgOtLUUUSpktHRspjhXw/140.jpg

2. Apple iPod touch, 3rd generation, 32GB

To begin with, I encoded the URL and tried to send it. But the server says status Forbidden

Any solution to this ?

UPDATE ::

It end up calling to

http://abc.com/addToWishlist.php?rand=506075547542422&image=http://thumbs1.ebaystatic.com/m/mO64jQrMqam2jde9aKiXC9A/140.jpg&prod=Flat%20USB%20Data%20Sync%20Charging%20Charger%20Cable%20Apple%20iPhone%204G%204S%20iPod%20Touch%20Nano

Javascript Code ::

function addToWishlist(num) {
var myurl = "addToWishlist.php";
var myurl1 = myurl;
myRand = parseInt(Math.random()*999999999999999);

var rand  = "?rand="+myRand ;
var modurl = myurl1+ rand + "&image=" + encodeURI(storeArray[num][1]) + "&prod=" + encodeURI(storeArray[num][0]);
httpq2.open("GET", modurl, true);
httpq2.onreadystatechange = useHttpResponseq2;
httpq2.send(null);
}
function useHttpResponseq2() {
if (httpq2.readyState == 4) {
if(httpq2.status == 200) {
var mytext = httpq2.responseText;
document.getElementById('wish' + num).innerHTML = "Added to your wishlist.";
}
}
}

Server Code

<?php
include('/home/ankit/public_html/connect_db.php');

$image = $_GET['image'];
$prod = $_GET['prod'];
$id = $_GET['id'];



echo $prod;
echo $image;
?>

As I mentioned, its pretty basics

More Updates :

On trying to send a POST request via AJAX to the server, it says :-

Refused to set unsafe header "Content-length"
Refused to set unsafe header "Connection"
19
  • 5
    Provide some code please Commented Nov 12, 2012 at 22:21
  • 1
    What url did the AJAX end up calling? Commented Nov 12, 2012 at 22:21
  • Is the URL handling your request accessible? It could as well reply 403 to anything regardless of the GET or POST params. Commented Nov 12, 2012 at 22:24
  • @JanDvorak If I remove the parameters, they are accessible Commented Nov 12, 2012 at 22:29
  • It still looks like a server issue. Can you post the server code? Commented Nov 12, 2012 at 22:32

2 Answers 2

5

2 things.

  1. Use encodeURIComponent() instead of encodeURI().

    Here is a detailed discussion on this: When are you supposed to use escape instead of encodeURI / encodeURIComponent?

  2. If you are new to JavaScript, use some lib to help you do the AJAX work. Like mootools, jQuery, etc.

Sign up to request clarification or add additional context in comments.

Comments

0

Using a POST request solved my issue :)

function addToWishlist(num) {
var url = "trial.php";
var parameters = "prod=" + encodeURIComponent(storeArray[num][0]) + "&image=" + encodeURIComponent(storeArray[num][1]);
httpq2.open("POST", url, true);
httpq2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpq2.onreadystatechange = function(){
if (httpq2.readyState == 4) {
if(httpq2.status == 200) {
var mytext = httpq2.responseText;
document.getElementById('wish' + num).innerHTML = "Added to your wishlist.";
}
}
};
httpq2.send(parameters);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.