3
function saveProjectAjax(docsId, content) {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        localStorage.setItem('upadateContent',JSON.stringify(content));
        if (this.readyState == 4 && this.status == 200) {
        }
    };

    xmlhttp.open("GET", "addProjectDetailBase.php?cu=true&pid=" + docsId+"&content="+encodeURIComponent(content), true);
    xmlhttp.send();
}

I want send my content (json) data from function which is large as of 250 kb through content parameter of my function

2
  • 2
    If you're going to send data to the back end, use POST instead of GET. POST is for sending data and GET is for getting data. You should also include your actual issue in the question. You have some code, what happens when you use it? Any error messages? Commented Jan 10, 2019 at 6:29
  • would you please provide me pure javascript code for post using ajax ? this may help me. :) Commented Jan 10, 2019 at 8:57

1 Answer 1

1

I agree with what @Magnus Eriksson said above. I will use POST instead of GET. Then I would use a key | value paired object and convert into a JSON string and send over to the server via 'POST'.

Here is an example below,

var xhr = new XMLHttpRequest();
var url = 'addProjectDetailBase.php'
xhr.open("POST", url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
    value: value
}));

Hope this helps,

Cheers.

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

4 Comments

Thanks @Anjana I tried it, but it is returning null, would you please provide me both code to send and access through javascript and php. :)
@NanheMaurya : Did you replace the value with your parameters?
@NanheMaurya - Instead of sending them as json, just send them in the same format you already have: xhr.send("cu=true&pid=" + docsId+"&content="+encodeURIComponent(content));. Then you should be able to use the $_POST-super global in PHP to fetch the data (just like a normal form post). Here's a post about how to post data using Ajax
@NanheMaurya Glad to hear it worked :) Could you please accept my answer then :) Thank you.

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.