相关疑难解决方法(0)

将JSON数据从Javascript发送到PHP?

如何在浏览器中将JSON数据从Javascript发送到服务器并让PHP在那里解析?

javascript php ajax json

45
推荐指数
3
解决办法
10万
查看次数

Ajax调用触发错误事件但返回200 ok

$.ajax({
        url: 'http://intern-dev01:50231/api/language',
        type: 'GET',
        dataType: 'json',
        success: function() {
            console.log('It Works!');
        },
        error: function (request,status, error) {
            console.log(error);
            alert(status);
        }
    });
Run Code Online (Sandbox Code Playgroud)

为什么这个ajax调用不起作用?如果我在浏览器中调用它可以正常工作:/.

这就是小提琴手的回报:

HTTP/1.1 200 OK
Content-Length: 122
Content-Type: application/json; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Fri, 26 Apr 2013 06:56:40 GMT

[{"LanguageId":1,"LanguageName":"Dansk"},{"LanguageId":2,"LanguageName":"Tysk"},{"LanguageId":3,"LanguageName":"Engelsk"}]
Run Code Online (Sandbox Code Playgroud)

javascript api jquery json

15
推荐指数
2
解决办法
3万
查看次数

JQuery ajax调用在200上执行错误

我的c#/ WebApi服务器代码如下所示:

[HttpPost]
public HttpResponseMessage Logout()
{
    // do some stuff here ... 
    return Request.CreateResponse(HttpStatusCode.OK);
}
Run Code Online (Sandbox Code Playgroud)

在客户端,我使用jquery 2.0.3进行ajax调用

ADDED:Jquery代码(typescript)......

    var ajaxSettings: JQueryAjaxSettings = {};
    ajaxSettings.type = 'POST';
    ajaxSettings.data = null;
    ajaxSettings.contentType = "application/json; charset=utf-8";
    ajaxSettings.dataType = "json";
    ajaxSettings.processData = false;

    ajaxSettings.success = (data: any, textStatus: string, jqXHR: JQueryXHR) => {
        console.log("Success: textStatus:" + textStatus + ", status= " + jqXHR.status);
    };
    ajaxSettings.error = (jqXHR: JQueryXHR, textStatus: string, errorThrow: string) => {
        console.log("Error: textStatus:" + textStatus + ", errorThrow = " …
Run Code Online (Sandbox Code Playgroud)

c# jquery

10
推荐指数
2
解决办法
5514
查看次数

dataType:"json"无效

我正在尝试使用数组中的json将多个变量从php文件发送回ajax.php文件中的代码完美运行,并且应该像我的数据库一样完成所有操作.但是当我在ajax中添加dataType:"json"时,php文件中就没有任何事情发生了.我google了一下,有些人提到它可能是一个浏览器问题,但到目前为止它无法在firefox,chrome或IE中使用.我正在使用最新版本的jQuery.

这是在php内部发生的事情:

<?php
//Create variables and update database

echo json_encode(array("id" => "$realid", "un" => "$username", "date" => "$date"));
?>
Run Code Online (Sandbox Code Playgroud)

这是ajax代码:

.ajax(
{
   url: 'UpdateComments.php',
   type: 'POST',
   dataType: "json",
   data: 
   {
      type: "add",
      comment: $("#comment").val(),
      id: videoID  
   },
   success: function (data) 
   {
       //Get the data variables from json and display them on page
   }
});
Run Code Online (Sandbox Code Playgroud)

我对此毫无头绪,任何建议都将不胜感激!

php ajax json

7
推荐指数
1
解决办法
1万
查看次数

Ajax,Django:状态 200 但抛出错误而不是成功

我正在尝试使用 ajax 发表评论,但它不起作用。当我按下按钮控制台未打印任何错误(如 404 等)时,未发布评论。错误返回的对象:

{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, ...} abort: ƒ (e) always: ƒ () catch: ƒ (e) done: ƒ () fail: ƒ () getAllResponseHeaders: ƒ () getResponseHeader: ƒ (e) overrideMimeType: ƒ (e) pipe: ƒ () progress: ƒ () promise: ƒ (e) readyState: 4 responseText: "?" setRequestHeader: ƒ (e,t) state: ƒ () status: 200 statusCode: ƒ (e) statusText: "OK" then: ƒ (t,n,r) proto : Object

在命令行中我看到:

“POST / HTTP/1.1” 200 5572

当我将按钮更改为“提交”时,它会使用正确的 JSON …

django ajax django-templates django-views

7
推荐指数
1
解决办法
364
查看次数

来自IIS的压缩不完整的XHR响应

我正在使用jQuery ajax加载脚本.

这是一个来自加载器代码的剪辑,它的价值.

var resources = ['knockout-3.2.0.js', 'fonts/fontawesome-webfont.eot', ... ];
var retryCount = 0;
load(0);
function load(i) {
  if (i == resources.length) {
    $("#progBar").width("100%");
    $("#progCaption").text("executing App/main...")
    var requireScript = document.createElement('script');
    requireScript.setAttribute("type", "text/javascript");
    requireScript.setAttribute("src", "Scripts/require.js");
    requireScript.setAttribute("data-main", "App/main");
    head[0].appendChild(requireScript);
  }
  else {
    $("#progBar").width(100 * i / resources.length + "%");
    var r = resources[i];
    switch (r.substring(r.lastIndexOf('.'))) {
      case ".js":
        $("#progCaption").text(r);
        $.getScript('Scripts/' + r)
        .done(function (data) {
          retryCount = 0;
          load(++i);
        })
        .error(function (err) {
          if (err.status == 404 || retryCount == 3) { …
Run Code Online (Sandbox Code Playgroud)

jquery fiddler getscript internet-explorer-11

5
推荐指数
1
解决办法
782
查看次数

.NET (ApiController) / jQuery .ajax:从 POST 返回什么?

在从 ApiController 派生的 Controller 内的 Post 方法中,我应该返回什么来指示 jQuery 成功?

我尝试过 HttpResponseMessage 但 jQuery 将此视为错误(即使 jQuery 错误处理程序的参数显然具有 200 状态)。

jQuery 看起来像这样:

processParticipantEvent: function(parID, evtType, evtNotes, successFunction, errorFunction){
    debugger;
    var requestURL = '/api/participantevent';
    var json = {"parId" : parID, "evtType": evtType, "evtNotes": evtNotes};
    var jsonArray=JSON.stringify(json);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: requestURL,
        data: jsonArray ,
        dataType: "json",
        success: function (data) { successFunction(data); },
        error: function (data) { errorFunction(data); }
    });
},
Run Code Online (Sandbox Code Playgroud)

我读过这个:Ajax 请求返回 200 OK,但是触发了一个错误事件而不是成功,这似乎涉及同一问题,但我怀疑它没有数据,因为它对我不起作用?

需要明确的是,我想做的就是返回一个没有数据的普通旧 2xx。

c# asp.net ajax jquery

3
推荐指数
1
解决办法
3795
查看次数

JSONP 的 jQuery.ajax() 返回 200,但解析错误运行错误回调而不是成功回调

首先,我知道网站上有类似的帖子,但没有一个能解决我的问题。

我想从 Web 服务器获取 JSON 文件。JSON 文件已由 JSONLint 检查,因此验证语法正确。JSON 数据是从类似于以下的 URL 检索的:www.examplewebsite.com/data.json

我的 jQuery Ajax 代码:

$.ajax({

    url:url",
    dataType:"jsonp",
    contentType: "application/json",
    type:"get",
    success:function(){
        console.log("OK");
    },
    error:function(data,status,error){
        console.log(data " "status+" "+error);
    }
});
Run Code Online (Sandbox Code Playgroud)

这是错误:

:parsererror 错误:未调用 jQuery222030055767520832166_1465026956736

到目前为止我所做的:

  • 添加了回调函数(jsonpCallback:fname)(我真的想知道,如果我们必须添加回调函数来到达服务器端JSON?)函数fname可以工作(alert("OK");),但我仍然收到错误。错误消息之一是“未调用未定义”
  • 添加了 jsonp:false 选项
  • 将 dataType:jsonp 更改为 json 但出现Access-Control-Allow-Origin错误
  • 仔细检查生成的 JSON 是否有效

更新1

我的json内容(用于测试):

[{
  "id": 1,
  "first_name": "Sara"
}, {
  "id": 2,
  "first_name": "Lois"
}, {
  "id": 3,
  "first_name": "Annie"
}, {
  "id": 4,
  "first_name": "Gregory"
}, { …
Run Code Online (Sandbox Code Playgroud)

ajax jquery json jsonp

3
推荐指数
1
解决办法
2595
查看次数