Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
why $.ajax dose n't work . and show error .

JavaScript
<script type="text/javascript" >
 
            {
                $(document).ready(function () {
                    alert('hi');
                    $('#CountryAndCity ').change(function () {
 
                        
                        var str = document.getElementById(' CountryAndCity ').value;
                        var str1 = str.split(", ", 1).toString();
                        var str2 = str.split(", ", 2).toString();
 

                        $.ajax({
                            url: '/AccountController/Myaction',
                            data: { countryy: str1, cityy: str2 },
                            cache: false,
                            type: 'GET',
                            success: function (result) {
              
                                $('hidMyModelid_city').val(result);
                                          

                            },

                            error: function (xhr, ajaxOptions, thrownError) {
                       
                                alert(thrownError);
                            },
                        });
 
                    })
                });
            }
 
        </script


C#
public ActionResult MyAction(string countryy, string cityy)
        {
            var result = MyClass.GetIDCity(countryy, cityy);
            return Json(result, JsonRequestBehavior.AllowGet);
            
        }

public static long GetIDCity (string contri, string citi)
        {
            using(SportContext db = new SportContext ())
            {
long Idcontri= db.tbl_country.FirstOrDefault(p => p.country == contri).id;
return db.tbl_city.FirstOrDefault(p => p.id_ country == Idcontri && p.city == citi).id;

            }
           
        }
Posted
Comments
ZurdoDev 5-Aug-13 20:54pm    
What's the error?

1 solution

If your code whatever you have posted here is correct, following line will produce an error -
1. Extra white space -
JavaScript
var str = document.getElementById(' CountryAndCity ').value;


And, the reason is extra whitespaces in argument to the document.getElementById() [http://jsfiddle.net/4Vsxy/[^]]
And, one advice here. If you are using jQuery writing this expression makes it odd as you can very well use
JavaScript
var str = $('#CountryAndCity').val();


2. If you code works, and you are getting error in ajax call you need to look into the details of GET request using firebug, fiddler or any other tool. That would not be much difficult.

3. If everything works fine and you are getting error due to getting null value in the hidden field "hidMyModelid_city" which you are using then this will work-
JavaScript
$('#hidMyModelid_city').val(result); 


as, you haven't included # in the selector you are providing (I guess its an id for sure).

I hope this would work for you. :)
Thanks
 
Share this answer
 
v2
Comments
Member 8454063 6-Aug-13 2:17am    
as you see , i put alert(thrownError);
and it shows "not found" .
and alert(xhr.status) shows 404 .
i think problem is ajax part . i installed firebug , i don't know do right or not, but when i put breakpoint on ajax part, shows seccuss . although it's script pannel says :"Script Panel was inactive during page load"
i don't know what's my ajax problem .
Ashutosh Mahto 6-Aug-13 3:35am    
Getting 404 means its not reaching to the routed url. Well, I can not say here what exactly is the issue, but you may recheck the URL '/AccountController/Myaction' whether its reaching to the right destination or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900