unexpected 's' in postman on sending Webmethod
Asked Answered
A

1

7

Integrating a payment Checkout Stripe. Used JavaScript Stripe handler, to apply Stripe charge on the transaction.
After charging the customer, it returns token. Using this token we can proceed for Actual payment.

And here is the AJAX call to Payment functionality:

var StripeHelper =
    {
        payProceed: function (token) {
            try {
                var _ajax = new AjaxHelper("/Services/Service.asmx/PaymentProceed");
                _ajax.Data = "{token:" + JSON.stringify(token) + "}";
                _ajax.OnInit = function () { PageHelper.loading(true); };
                _ajax.OnSuccess = function (data) {
                    console.log(data.d);
                    PageHelper.loading(false);
                    window.location('/payment-success');
                };
                _ajax.Init();
            }
            catch (e) {
                PageHelper.loading(false);
            }
        }
    }

Here is Web Method on my TEST server, which passes token to Stripe server:

[WebMethod(EnableSession = true)]
    public string PaymentProceed(string token)
    {
        Session["PAYMENT_MODE"] = PaymentContants.PaymentVia.Stripe;
        var myCharge = new StripeChargeCreateOptions();
        myCharge.AmountInCents = 100;
        myCharge.Currency = "USD";
        myCharge.Description = "Charge for property sign and postage";
        myCharge.TokenId = token;
        string key = "sk_test_Uvk2cH***********Fvs"; //test key

        var chargeService = new StripeChargeService(key);
        StripeCharge stripeCharge = new StripeCharge();
        //Error is coming for below line -->
        stripeCharge = chargeService.Create(myCharge);
        //No token found

        return stripeCharge.Id;
    }

If I POST AJAX call on POSTMAN, it shows

unexpected 's' in JSON.

What it means in general, and in this case specifically?

Accompany answered 26/9, 2016 at 13:25 Comment(8)
It looks like postman (or the system generating that message) tries to interpret the response text as JSON and encounteres an 's' where it didn't expect it. What is the response text? And who gives that message - postman or stripe?Lycopodium
stripe simply returns Payment failed. Postman gives this message.Accompany
So, what is that response body exactly? Or maybe, what is the value of that _ajax.Data? Don't you need quotes around the stringified token?Lycopodium
_ajax.Data is simply a token: alphanumeric key. And I think quotes are not required if stringify is done.Accompany
@Vikrant, you are returning a string here and then the headers might be saying application/json and that is why the issue occurs. There is a code button which will show you sample code for curl as shown in i.sstatic.net/2FQYO.png. Do that an add a -v after curl to enabled verbose mode. Then execute that command in terminal and share the output of the same in your questionPossession
In the Response panel of Postman, try to switch to Raw or Preview. You are probably in the Pretty mode using the JSON option and the response you get from Stripe is not a valid JSON and it is starting with an 's'. I would also try this in your code: _ajax.Data = "{ \"token\": \"" + token + "\"}";Farriery
@tehCivilian, I tried that as well. Got no change in response!Accompany
@Accompany then probably your method expects json but you don't have valid one - your key in the code starts with 's', are you sure you are managing your data correctly?Rickierickman
F
1

Problem is in this line

_ajax.Data = "{token:" + JSON.stringify(token) + "}";

You should use

var token = '"Your token with S goes here"';
_ajax.Data = "{token:" + JSON.stringify(token) + "}";

because only token is not valid json and you are trying to parse it

See the Reference

Frei answered 19/6, 2018 at 11:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.