A
A
adun32015-10-04 11:53:13
JavaScript
adun3, 2015-10-04 11:53:13

How to make a replacement when sending an ajax request?

Good afternoon!
I want to calculate the shipping cost using mail api.
The response from the server comes in the form:

{"rsp":{
"stat":"ok",
"locations":[
{"value":"city--abakan","name":"АБАКАН","type":"cities"},
{"value":"city--anadyr","name":"АНАДЫРЬ","type":"cities"},
... ,
{"value":"city--yaroslavl","name":"ЯРОСЛАВЛЬ","type":"cities"}
]}}

I load them in the select-a options:
function (data) {
    for (i = 0; i < data.rsp.locations.length; i++){
        $("#from").append("<option>" + data.rsp.locations[i].name);
        $("#to").append("<option>" + data.rsp.locations[i].name);

Then when it is necessary to calculate the cost, and it is necessary to send a request,
nothing comes.
you can do it not by the name field, but by the value field, then everything works,
but it doesn’t look beautiful (cities are not in Russian, but in transliteration).
I check which option is pressed and send the request.
$.ajax({
            url: "http://emspost.ru/api/rest/",
            dataType : "jsonp",
            data: ({
            method:"ems.calculate",
            from: $("#from option:selected").val(),
            to: $("#to option:selected").val(),
            weight: $("#weight").val()
            }),
        success: 
        function (data) {
        if (data.rsp.stat == "fail"){
            $("#raschet").empty().html("Ошибочный вес посылки");
            $("#time").empty();
        }
        else {
            $("#raschet").empty().html("Цена доставки: " + data.rsp.price);
            $("#time").empty().html("время доставки: " + data.rsp.term.min + "-" + data.rsp.term.max + " дней");          
        }

how to make it so that it is displayed by the name field, and the calculation is by the value field?
ps in jquery 2nd day :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
littleguga, 2015-10-04
@adun3

Add the data-val attribute and write the city code in it, and the name in the option.
Like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question