0

Please refer below code

var pdf="xxxxxxx";

 $.ajax({
            type: "post",
            url: "/Corporate/SampleChangeSummary/PDF",
            data: JSON.stringify(pdf),
            contentType: 'application/json',
            dataType: 'json',
            error: function (status, xhr) {

            }
        });


[HttpPost]
        public ActionResult PDF(object pdf)
        {
.......
}

but it returning pdf is null. What is the problem? Any syntax mistake in my code?

4 Answers 4

1

I believe that contentType is what causing the issue. In that script of yours, you were telling your $.ajax() that it is sending JSON string, but you had it sending a .pdf. Instead, just remove contentType line and try it.

Secondly, dataType: 'json' is telling your $.ajax() that it is expecting the response in JSON format. If that's what you are doing, then you dont have to worry about that line.

EDITED:

Found more information - if you wanted to keep contentType then, revise to this: contentType: application/pdf', and add header('Content-type: application/pdf'); on your php file that your $.ajax() is sending to.

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

Comments

0

Try this:

$.ajax({
    type: "post",
    url: "/Corporate/SampleChangeSummary/PDF",
    data: JSON.stringify({pdf: pdf}),
    contentType: 'application/json',
    dataType: 'json',
    error: function (status, xhr) { }
});

ASP.NET MVC requires you to send a 'key-value' JSON object so it can bind accordingly. Also, change object pdf to string pdf.

2 Comments

Also, try it: contentType: 'application/json; charset=utf8'
If this is not working, then you're doing something really weird there.
0

Try somthing like this

$.ajax({
         type: "POST",
         url: '/Corporate/SampleChangeSummary/PDF',
         data: { pdf: JSON.stringify(pdf) },
         success: function (data) {
                    //do somthing with the result
                  },
         error: function (error) {
                    alert('Please try again!');
                }
        });

Comments

0
  var pdf="xxxxxxx";

   $.ajax({
        type: "post",
        url: "/Corporate/SampleChangeSummary/PDF",
        data: { "pdf" : pdf },
        dataType: 'json',
        error: function (status, xhr) {

        }
    });


  [HttpPost]
    public ActionResult PDF(object pdf)
    {
       .......
    }

you don't need to stringify your pdf because it is already string try to pass it directly to your controller.

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.