0

I am making the following jquery ajax call to a codeigniter php function:

    $.ajax({
                        type:"POST",
                        url: "Ajax/getHtml",
                        data: { u : 'http://stackoverflow.com/' },
                        contentType: "application/json; charset=utf-8",
                        dataType: 'html',       
                        success: function(data) {
                            console.log(data);

                        },

                        error: function(jqXHR, textStatus, errorThrown) {
                                console.log('error');
                                console.log(jqXHR,textStatus, errorThrown);
                        }
                    });

The requested php function is :

public function getHtml() {
        var_dump($_POST);
        $url = $_POST['u'];
        $result = file_get_contents($url);
        echo ($result);
}

var_dump($_POST) yields:

array(0) { }

How can I fix this?

3
  • 1
    setting contentType option is telling the request what data type you are sending to the server, php by default doesnt know how to receive/process json, you would have to read the raw input to get it. Remove the contentType option Commented Jan 25, 2015 at 5:17
  • Have you tried changing content type? Commented Jan 25, 2015 at 5:17
  • you don't need to set the content type, omit that part and it should work fine Commented Jan 25, 2015 at 5:17

1 Answer 1

1

Php will not populate the $_POST array if the content type of the request is application/json; charset=utf-8, also you aren't sending json. Just remove the content type line and the proper(default) content type of application/x-www-form-urlencoded will be set.

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

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.