Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Javascript进行Ajax调用_Javascript_Php_Jquery_Ajax - Fatal编程技术网

如何使用Javascript进行Ajax调用

如何使用Javascript进行Ajax调用,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我需要的是: 我需要使用javascript发送ajax调用。 我已经将jquery用于ajax调用。 Jquery代码: var mobile = $("#mobiledropdown".attr("value"); url = window.location.href.indexOf("?") > -1 ? document.URL + "&asyn=1" : document.URL + "?asyn=1", jQuery.support.cors = !0, $.ajax(

我需要的是:

我需要使用javascript发送ajax调用。 我已经将jquery用于ajax调用。 Jquery代码:

var mobile = $("#mobiledropdown".attr("value");
url = window.location.href.indexOf("?") > -1 ? document.URL + "&asyn=1" : document.URL + "?asyn=1", jQuery.support.cors = !0, $.ajax({
type: "GET",
url: mobile,
cache: !1,
dataType: "json",
crossDomain: !0,
success: function(t) {
    if (t.industry.length > 0) {
        industry = "{{ page_param.page.industry_url }}";

        for (var e = 0; e < t.industry.length; e++)

        $("#industry").append("" != industry && t.industry[e].industry_url == industry ? "<option value=" + t.industry[e].industry_url + " selected >" + t.industry[e].name + " </option>" : "<option value=" + t.industry[e].industry_url + ">" + t.industry[e].name + " </option>")

    }

    else $("#industry").addClass("off").css({
        "background-color": "#f4f4f4",
        "border-color": "#eaeaea",
        color: "#ccc"
    }), $("#industry").attr("disabled", "");
    if (t.country.length > 0) {
        country = "{{ page_param.page.country_url }}";
        for (var e = 0; e < t.country.length; e++) $("#country").append("" != country && t.country[e].country_url == country ? "<option value=" + t.country[e].country_url + " selected >" + t.country[e].text + " </option>" : "<option value=" + t.country[e].country_url + ">" + t.country[e].text + " </option>")
    } else $("#country").addClass("off"), $("#country").attr("disabled", "");
    if (t.city.length > 0) {
        city = "{{ page_param.page.city_url }}";
        for (var e = 0; e < t.city.length; e++) $("#city").append("" != city && t.city[e].city_url == city ? "<option value=" + t.city[e].city_url + " selected >" + t.city[e].text + " </option>" : "<option value=" + t.city[e].city_url + ">" + t.city[e].text + " </option>")
    } else $("#city").addClass("off").css({
        "background-color": "#f4f4f4",
        "border-color": "#eaeaea",
        color: "#ccc"
    }), $("#city").attr("disabled", "");
    if (t.type.length > 0) {
        type = "{{ page_param.page.event_type }}";
        for (var e = 0; e < t.type.length; e++) 1 == t.type[e].entityType && $("#shows").append("" != type && t.type[e].entityType == type ? "<option value=" + t.type[e].entityType + " selected > Trade Shows </option>" : "<option value=" + t.type[e].entityType + "> Trade Shows </option>"), 2 == t.type[e].entityType && $("#shows").append("" != type && t.type[e].entityType == type ? "<option value=" + t.type[e].entityType + " selected > Conferences </option>" : "<option value=" + t.type[e].entityType + "> Conferences </option>")
    }
    $("#shows").append("<option value= 3 > Venues </option>"), $("#shows").append("<option value= 4> Organizers </option>"), $("#shows").append("<option value= 5> Top100 </option>")
}
});
Javascript ajax调用不起作用 代码

HTML元素

  <span id="mobiledropdown" value="{{-p|raw}}" onload="loadFilters();"></span>
问题

xhr请求不起作用

我是使用javascript的ajax新手

jqueryajax调用运行良好,但使用javascript的ajax不起作用

我想要javascript如何在数据类型中发送json,在javascript中跨域发送json

欢迎提出任何建议

语法:

jQuery.ajax({
    type: "GET",
    async: false, // In your code you're waiting the req. I dont know why.
    url: "http://someUrl", // Remote URL at which you sending the request
    success: function(response) {
        // Here you can handle the response from the server.
    },
    error: function(error) {
        // Here you handle if the HTTP request fails (if status is > 299)
    }
})
如果您只是没有设置async,那么默认值为true,因此它将发送请求,并且您的代码将继续执行。响应可用后,将调用success函数。我建议使用async:true,或者不要在选项中定义它。如果您不尝试访问success函数之外的响应数据,则不需要它

如果此变量未被其他库或其他代码覆盖,则可以使用$代替jQuery

在jQuery对象内部,您可以放置许多其他缓存、请求、

您可以用$.ajax{}替换所有基本javascript的XMLHttpRequest功能

您可以使用速记方法$.get

就你而言:

$.get('mobile', function(response) {
    // Here you can handle the response when it come from the server.
})

格式化代码、缩进和其他内容。这真的是一个意大利面,我真的不知道像industry这样的代码是不是{{page_param.page.industry_url};也许会有帮助,为什么不使用jQuery的$.ajax呢?我不知道你是否理解我的意思。删除所有与AJAX无关的废话。使用控制台来发现可能的错误,然后返回这里,用最少的代码来重现这个问题。我正在提高页面速度,所以我不想包含juqery lib,这就是为什么我切换到javscriptOK的原因。好吧,用纯JavaScript调用AJAX并不难,只是我真的不认为我们需要$city.attrdisabled之类的东西;来帮助你。
$.get('mobile', function(response) {
    // Here you can handle the response when it come from the server.
})